Spring5学习笔记4MVC框架整合
Spring5学习笔记4——MVC框架整合
配套视频参考:孙哥说Spring5
第四部分:MVC框架整合
第一章、MVC框架整合思想
1. 搭建Web运行环境
1 | <dependency> |
2. 为什么要整合MVC框架
- MVC框架提供了控制器(Controller)调用Service
DAO —》 Service - 请求响应的处理
- 接受请求参数 request.getParameter(“”)
- 控制程序的运行流程
- 视图解析 (JSP JSON Freemarker Thyemeleaf )
3. Spring可以整合哪些MVC框架?
- struts1
- webwork
- jsf
- struts2
- springMVC
4. Spring整合MVC框架的核心思路
4.1 web环境下的工厂创建:
1 | 1. Web开发过程中如何创建工厂 |
4.2 代码整合
依赖注入:把Sevice对象注入个控制器对象。
第二章、Spring与Struts2框架整合 (选学)
1. Spring与Struts2整合思路分析:
Struts2中的Action需要通过Spring的依赖注入获得Service对象。
2. Spring与Struts2整合的编码实现
-
搭建开发环境
-
引入相关jar (Spring Struts2)
1
2
3
4
5<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
-
-
引入对应的配置文件
- applicationContext.xml
- struts.xml
- log4j.properties
- applicationContext.xml
-
初始化配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<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)
-
编码
-
开发Service对象
1
2最终在Spring配置文件中创建Service对象
<bean id="userService" class="com.baizhi.struts2.UserServiceImpl"/>
-
-
开发Action对象
-
开发类
1
2
3
4
5
6
7
8
9public class RegAction implements Action{
private UserService userService;
set get
public String execute(){
userService.register();
return Action.SUCCESS;
}
} -
Spring (applicationContext.xml)
1
2
3<bean id="regAction" class="com.baizhi.struts2.RegAction" scope="prototype">
<property name="userService" ref=""/>
</bean- Struts2(struts.xml)
1
2
3
4
5
6
7<package name="ssm" extends="struts-default">
url reg.action ---> 会接受到用户的请求后,创建RegAction这个类的对象 进行相应的处理
<action name="reg" class="regAction">
<result name="success">/index.jsp</result>
</action>
</package>
-
3. Spring+Struts2+Mybatis整合(SSM)
1. 思路分析
1 | SSM = Spring+Struts2 Spring+Mybatis |
2. 整合编码
-
搭建开发环境
-
引入相关jar (Spring Struts2 Mybatis)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</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>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<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-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<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.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>
-
-
引入对应的配置文件
- applicationContext.xml
- struts.xml
- log4j.properties
- xxxxMapper.xml
- applicationContext.xml
-
初始化配置
- Spring (ContextLoaderListener —> Web.xml)
- Struts2(Filter —> Web.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<!-- 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>
<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)
-
编码
-
DAO (Spring+Mybatis)
1
2
3
4
5
6
7
8
9
10
11
121. 配置文件的配置
1. DataSource
2. SqlSessionFactory ----> SqlSessionFactoryBean
1. dataSource
2. typeAliasesPackage
3. mapperLocations
3. MapperScannerConfigur ---> DAO接口实现类
2. 编码
1. entity
2. table
3. DAO接口
4. 实现Mapper文件
-
-
Service (Spring添加事务)
1
2
3
4
5
61. 原始对象 ---》 注入DAO
2. 额外功能 ---》 DataSourceTransactionManager ---> dataSource
3. 切入点 + 事务属性
@Transactional(propagation,readOnly...)
4. 组装切面
<tx:annotation-driven-
Controller (Spring+Struts2)
1
- 开发控制器 implements Action 注入Service
- Spring的配置文件
- 注入 Service
- scope = prototype
- struts.xml
1
-
4. Spring开发过程中多配置文件的处理
1 | 实战中会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。 |
-
通配符方式
1
2
3
4
5
6
71. 非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> -
import标签
1
2
3
4
5
6
7
8
9
10
11
12applicationContext.xml中添加以下标签:(目的:整合其他配置内容)
<import resource="applicationContext-dao.xml " />
<import resource="applicationContext-service.xml " />
<import resource="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>
评论