Spring MVC框架整合
文章目录
1.MVC框架整合思路
1.1 搭建Web运行环境
<dependencies>
<!-- 单元测试 作用域测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- servlet api 作用域 编译 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- servlet jsp 作用域 编译 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Spring webmvc相关jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- Spring 事务支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- Spring jdbc支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- Mybatis Spring 整合jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- jdbc mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- log4j 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- log4j 日志门面slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha0</version>
</dependency>
</dependencies>
1.2 为什么要整合MVC框架
1. MVC框架提供了控制器(Controller)调用Service
DAO ----> Service ---->
2. 请求响应的处理
3. 接收请求参数 request.getParameter();
4. 控制程序的运行流程
5. 试图解析(JSP JSON Freemarker Thyemeleaf)
1.3 Spring 可以整合哪些MVC框架
1. struts1
2. webwork
3. jsf
4. struts2
5. springMVC
1.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(唯一) AppcationContext 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>
-
代码整合
依赖注入:把Service对象注入给控制器对象.
2 Struts2与Spring框架整合(选学)
2.1 Spring与Struts2整合思路分析
1. Struts2中的Action需要通过Spring的依赖注入获得Service对象.
2.2 Spring与Struts2整合的编码实现
-
搭建开发环境
-
映入相关jar(Spring Struts2)
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.8</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:application.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)
- Struts(Filter --> Web.xml)
-
编码
-
开发Service对象
最终在Spring配置文件中创建Service对象 <bean id = "userService" class = "com.leetao.service.UserServiceImpl"/ >
-
开发Action对象
-
开发类
public class RegAction implements Action{ private UserService userService; set get public String execute(){ userService.register(); return Action.SUCCESS; } }
-
Spring(applicationCcontext.xml)
<bean id = "regAction" class = "com.leetao.action.RegAction" scope = "prototype"> <property name = "userService" ref="."/> </bean>
-
Struts2(struts.xml)
<package name="ssm" extends="struts-default"> <!-- url reg.action ->接收到用户的请求后,创建RegAction这个类的对象,进行相应的处理 --> <action name="reg" class="regAction"> <result name="success">/index.jsp</result> </action> </package>
-
-
2.3 Spring+Struts2+MyBatis整合(SSM)
-
思路分析
SSM = Spring+Struts + Spring+MyBatis
-
整合编码
-
搭建开发环境
-
映入相关jar(Spring Struts2)
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!-- Spring framework and Servlet jsp jstl -->
-
-
引入对应的配置文件
- applicationContext.xml
- struts.xml
- log4j.properties
- *mapper.xml
-
初始化配置
- Spring(ContextLoaderListener -->Web.xml)
- Struts(Filter --> Web.xml)
<!-- Spring工厂 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <!-- Struts2执行拦截器 --> <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>
-
编码
-
DAO(Spring+Mybatis)
1. 配置文件的配置 1.DataSource 2.SqlSessionFactory --->SqlSessionFactoryBean 1.dataSource -->数据源 2.typeAliasesPackage -->实体别名 3.mapperLocations --->映射文件 3.mapperScannerConfigurer--->DAO接口 2. 编码 1. entity 2. table 3. DAO接口 4. 实现Mapper文件
-
Service(Spring添加事务)
1. 原始对象 -->注入DAO 2. DataSourceTransactionmanager --->dataSource 3. 切入点+事务属性 @Transactional(propagation,readOnly..) 4. 组装切面 <tx:annotation-driven/>
-
Controller(Spring+Struts2)
1.开发控制器 implements Action 注入service 2. Spring的配置文件 1.注入 Service 2 scope = prototype 3.Struts.xml <action class="Spring配置文件中action对应的id值"/>
-
-
3 Spring与SpringMVC整合
4 Spring开发过程中多配置文件的处理
Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护.
DAO ------ applicationContext-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="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>
异常汇总
NoSuchMethodError 没有这样的方法错误
NoClassDefFoundError 没有找到class错误
检查包是否导入 ?版本是否匹配?