1. 程式人生 > >spring和springmvc整合案例

spring和springmvc整合案例

專案原始碼傳送門:http://download.csdn.net/download/six_666666/10037348

專案框架


原始碼分享

Spring.java

package com;


public interface Spring {  
    void get();  
}  

SpringManager.java

package com;


public class SpringManager implements Spring {  
  
    public void get() {  
        System.out.println("我的整合成功!");  
    }    
}  

UserController.java

package com;   
import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.context.WebApplicationContext;  
import org.springframework.web.context.support.WebApplicationContextUtils;  
import org.springframework.web.servlet.support.RequestContextUtils;  
  
  
@Controller  
@RequestMapping("/user")  
public class UserController {  
 
    @Resource(name="springManager")  
      
    private Spring springManager;  
    @RequestMapping("/toSuccess.do")  
    public String ToSuccess(HttpServletRequest request){  
        //spring上下文  
        WebApplicationContext ac1 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());  
        //springmvc的上下文  
        WebApplicationContext ac2=RequestContextUtils.getWebApplicationContext(request);  
        springManager.get();  
        return "success";  
    }  
}  

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [  
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">  
]>
<beans>
<bean id="springManager" class="com.SpringManager"></bean>
</beans> 

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd   
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">


<!-- mvc的註解驅動 -->
<mvc:annotation-driven />
<!-- 一旦有掃描器的定義mvc:annotation-driven不需要,掃描器已經有了註解驅動的功能 -->
<context:component-scan base-package="com" />




<!-- 字首+ viewName +字尾 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- webroot到某一指定的資料夾的路徑 -->
<property name="prefix" value="/WEB-INF/jsps/"></property>
<!-- 檢視名稱的字尾 -->
<property name="suffix" value=".jsp"></property>
</bean>


<!-- id="multipartResolver"必須是multipartResolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- maxUploadSize:檔案上傳的最大值以byte為單位 -->
<property name="maxUploadSize" value="1024000"></property>
</bean>


<!-- <mvc:interceptors> <mvc:interceptor> 某一模組的攔截:/myPath/**, 攔截所有的請求/** 
<mvc:mapping path="/**"/> <bean class="cn.itcast.springmvc.interceptor.MyIntercepor"></bean> 
</mvc:interceptor> </mvc:interceptors> -->
</beans>  

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springMVC8</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
<!-- <param-value>classpath*:config/springAnnotation-servlet.xml</param-value> -->
</context-param>


<!-- 配置spring啟動listener入口 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springMVC啟動DispatcherServlete入口 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath*:config/springAnnotation-core.xml</param-value> -->
<param-value>classpath*:config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- encoding filter for jsp page -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>成功</title>
</head>
<body>
我的整合成功啦!!!
</body>
</html>

測試