admin管理员组文章数量:1794759
spring和spring MVC整合
spring和springMVC之间的整合,springMVC中的jar包包含spring中的jar包,所以无需再另外导入jar包,只需导入springMVC的jar包即可。
如图一所示:
这个时候,再新建两个源文件夹,一个为config专门放配置文件,另外一个为test,专门用来放进行测试的程序,在这个整合的小项目中,本身
并没有用上test源文件夹,但是加上倒也不错。
现在,在config文件夹中新建两个xml文件,一个为spring的配置文件,为applicationContext.xml,另一个为springmvc 的配置文件为springmvc.xml,两个xml
文件的配置如下:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "www.springframework/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <bean id="springManager" class="cn.controller.SpringManager"></bean> </beans> springmvc.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-3.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-3.0.xsd "> <!-- mvc的注解驱动 --> <mvc:annotation-driven/> <!-- 一旦有扫描器的定义mvc:annotation-driven不需要,扫描器已经有了注解驱动的功能 --> <context:component-scan base-package="cn.controller"/> <!-- 前缀+ 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.multipartmons.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.xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xmlns:web="java.sun/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置spring启动listener入口 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置springmvc启动dispatcherServlet入口 --> <!-- 中央控制器 --> <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: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> <!-- struts习惯使用/*,在springmvc不管用 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 现在建立springmvc.xml中扫描的包,具体包名与类名如下:下面依次为各个程序的代码:
ISpring:
package cn.controller; public interface ISpring { void get(); } SpringManager: package cn.controller; public class SpringManager implements ISpring { public void get() { System.out.println("spring和spring mvc整合成功!"); } } UserController: package cn.controller; 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 ISpring 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"; } } 如此,spring和springmvc的整合完成了,现在让我们将这个项目跑起来看一下到底成功了没有。debug调试观察ac1与ac2:
如下图所示:
spring上下文:
springmvc上下文:
jsp页面为:
如此,spring和springmvc的整合就成功了。
版权声明:本文标题:spring和spring MVC整合 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686618794a86941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论