Spring 整合 MVC
目录
- MVC 框架整合思想
- 为什么要整合 MVC 框架
- 搭建 Web 运行环境
- Spring 整合 MVC 框架的核心思路
- 1. 准备工厂
- 2. 代码整合
- Spring 整合 Struts2
MVC 框架整合思想
为什么要整合 MVC 框架
- MVC 框架提供了控制器(Controller)调用 Servlet
DAO —> Service - 请求响应的处理
- 接收请求参数
request.getParameter("")
- 控制程序的运行流程
- 视图解析(JSP、JSON、Freemarker、Thymeleaf)
Spring 可以整合哪些 MVC 框架?
- struts1
- webwork
- jsf
- struts2
- springMVC
搭建 Web 运行环境
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.1.14.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.1.14.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.1.14.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.1.14.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.8</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.3</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.43</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version><scope>provided</scope></dependency></dependencies>
Spring 整合 MVC 框架的核心思路
1. 准备工厂
Web 开发过程中如何创建工厂?
ApplicationContext ctx = new WebXmlApplicationContext("/applicationContext.xml");
如何保证工厂唯一,同时被共用?
- 共用:工厂存储在
ServletContext
这个作用域中,ServletContext.setAttribute("xxx", ctx);
- 唯一:在
ServletContext
对象创建的同时创建工厂。ServletContextListener
在ServletContext
对象创建的同时,被调用(只会被调用一次),
把创建工厂的代码写在ServletContextListener
中,也会保证只调用一次,保证了工厂的唯一性。
Spring 封装了一个 ContextLoaderListener
,主要做了两件事:
- 创建工厂
- 把工厂存在
ServletContext
中
ContextLoaderListener
使用方式:web.xml 中
<listener><listener-class> org.springframework.web.context.ContextLoaderListener </listen-class>
</listener><context-param><param-name> contextConfigLocation </param-name><param-value> classpath:applicationContext.xml </param-value>
</context-param>
2. 代码整合
Spring 整合 Struts2
这个选学。。。因为现在 Struts2 用的不多了。。暂时不学了