spring和spring MVC整合

来源:互联网 发布:防恶意代码软件 编辑:程序博客网 时间:2024/05/02 04:17

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" "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="cn.com.controller.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="cn.com.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.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.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?><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">  <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.com.controller;public interface ISpring {void get();}
SpringManager:

package cn.com.controller;public class SpringManager implements ISpring {public void get() {System.out.println("spring和spring mvc整合成功!");}}
UserController:

package cn.com.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的整合就成功了。

3 0