Spring系列——MVC框架整合
第一章 MVC框架整合思路
1、环境搭建
2、为什么要整合mvc框架
1. MVC框架提供了控制器(Controller)调用Service
DAO --> Service
2. 请求相应的处理
3. 接受请求参数 request.getParamter("")
4. 控制程序的运行流程
5. 视图解析(JSP JSON Freemarker Thyemeleaf)
3、Spring可以整合哪些MVC框架
1. struts1
2. webwork
3. jsf
4. struts2
5. springMVC
4、Spring整合MVC框架的核心思路
-
准备工厂
1. web开发过程中如何创建工厂 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); WebXmlApplicationContext 2. 如何保证工厂唯一同时被共用 被共用:web request|session|ServletContext(application) 工厂存储在servletContext这个作用域中,ServletContext.setAttribute("xxx",ctx); 唯一:ServletContext对象 创建同时 -->ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); ServletContextListener -->ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); ServletContextListener 在servletContext对象创建的同时,被调用(只会被调用一次),把工厂创建的代码,写在ServletContextListener中,也会保证只调用一次,最终工厂就保证了唯一性 3. 总结 ServletContextListener(唯一) ApplicationContext ctx = new WebXmlApplicationContext("/applicationContext.xml"); ServletContext.setAttribute("xxx",ctx)(共用) 4. Spring封装了一个ContextLoaderListener 1. 创建工厂 2.把工厂存在ServletContext中ContextLoaderlistener使用方式 web.xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml,/WEB-INF/applicationContext.xml</param-value> </context-param> -
代码整合
依赖注入:把service对象注入给控制器对象
第二章 Spring与struts2框架整合
1、Spring与struts2整合思路分析
struts2中的action需要通过spring的依赖注入获得service对象
2、Spring与struts2整合的编码实现
-
搭建开发环境
-
引人相关jar(Spring Struts2)
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.22</version> </dependency> -
引入对应的配置文件
- applicationContext.xml
- struts.xml
- log4j.properties
-
初始化配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>- Spring(ContextLoaderListener --> web.xml)
- Struts2(Filter --> web.xml)
-
3、Spring+Struts2+Mybatis整合
-
思路分析
Spring+Struts2 Spring+Mybatis -
搭建环境
- 引入对应的配置文件
- applicationContext.xml
- struts.xml
- log4j.properties
- xxxMappwe.xml
- 初始化配置
- Spring(ContextLoaderListener --> web.xml)
- Struts2(Filter --> web.xml)
- 引入对应的配置文件
4、Spring开发过程中的多文件配置
Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理和维护
DAO ---> applicationContext.xml-dao.xml
Service ---> applicationContext-service.xml
Action ---> applicationContext-action.xml
注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合
-
通配符方式
1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-*xml"); 2. web环境 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> -
标签
applicationContext.xml 目的 整合其他配置文件 <import resource="pplicationContext.xml-dao.xml"/> <import resource="pplicationContext.xml-service.xml"/> <import resource="pplicationContext.xml-action.xml"/> 1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 2. web环境 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>