概述
MVC框架整合思想
- 1、搭建开发环境
- 2、为什么要整合MVC框架
- 3、Spring可以整合那些MVC框架
- 4、Spring整合MVC框架的核心思路
- 1. 准备工厂
- 2. 代码整合
1、搭建开发环境
<dependencies>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- 整合log4j | 屏蔽默认的日志框架 -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!--mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
实际上Spring-webmvc的依赖,因为已经包含了Spring-web了
2、为什么要整合MVC框架
1. MVC框架提供了控制器(Controller)调用Service
DAO ---》Service
2. 请求响应的处理
3. 接受请求参数request.getParameter("")
4. 控制程序的运行流程
5. 视图解析(JSP JSON Freemarker Thyemeleaf)
3、Spring可以整合那些MVC框架
1. struts1
2. webwork
3. jsf
4. struts2
5. springMVC
4、Spring整合MVC框架的核心思路
1. 准备工厂
1.Web开发过程中如何创建工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
---> 替换为 WebXmlApplicationContext()
2.如何保证工厂唯一且同时被共用
被共用: Web request|session|ServletContext(application)
工厂存储在ServletContext这个作用域中ServletContext.setAttribute("xxxx" ,ctx);
唯一:ServletContext对象创建的同时
---> ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xm1");
怎么知道ServletContext对象什么时候创建 ---> 监听器:
ServletContextListener
---> ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xm1");
ServletContextListener在ServletContext对象创建的同时,被调用(只会被调用一次),把工厂创建的代码,写在 ServletContextListener中, 也会保证只调用一次,最终工厂就保证了唯一性
3.总结
ServletContextListener(唯一)
ApplicationContext ctx = new ClassPathXmlApplicationContext("/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>classpath:applicationContext.xml</param-value>
</context-param>
2. 代码整合
依赖注入:把Sevice对象注入个控制器对象。
最后
以上就是开心外套为你收集整理的Spring学习笔记:MVC框架整合的全部内容,希望文章能够帮你解决Spring学习笔记:MVC框架整合所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复