1、综述
所谓的SSM框架的整合即对Spring、Spring MVC和MyBatis这三个框架进行整合。由于从Web 3.0开始,使用Java开发Web项目时是可以避免使用和配置web.xml文件的,而是用Java Config的Java代码方式进行配置。
但我在之前的系列博客《Spring使用篇系列博客传送门》中曾讨论过类似的问题,答案是在使用第三方工具时,由于对第三方工具的源码不了解,因此推荐使用XML的配置方式使用,而对自己创建或维护的类,建议使用Java Config的配置方式。
因此在对SSM框架进行整合的过程中,不仅保留了对web.xml的配置,还使用了Java Config配置类,并在web.xml中对Java Config配置类进行了支持。采用这种整合方式的好处就是层次分明,而且更容易使我们理解对SSM的整合。
2、环境搭建
2.1 案例需求
为了使我们更好的理解对SSM的整合,我们在整合框架的过程中开发了一个Demo,该Demo的需求就是通过表单采集用户的姓名和年龄,并将信息添加到数据库的数据表中。
2.2 数据库搭建
创建名为“ssm”的数据库,数据库的字符集设置为UTF-8。在该数据库中创建名为“student”的表,其表的设计具体如下所示:
2.3 创建Web项目
在IDEA中创建名为“SSMConcordanceDemo”的Web Project,并将该项目发布到Tomcat上。该项目的具体项目结构如下所示:
- config包:放置Java Config配置类
- controller包:放置Spring MVC的控制器类
- dao包:放置DAO层接口
- mapper包:放置与DAO层接口相对应的MyBatis SQL映射文件
- pojo包:放置POJO类
- service包:放置Service层接口
- service.impl包:放置对应的Service接口的实现类
- resources包:放置各类配置文件
- lib文件夹:放置素有项目依赖的jar包
2.4 导入所需jar包
该项目所需的所有jar包全部存放在WEB-INF/lib文件夹中,其所有的jar包与系列博客《Spring使用篇(十)—— Spring与MyBatis整合》中使用的jar包相同,具体如下:
2.5 配置Log4j
在src下创建名为“log4j.properties”属性文件,具体的配置如下:
# Global logging configuration
#\u5728\u5F00\u53D1\u73AF\u5883\u4E0B\u65E5\u5FD7\u7EA7\u522B\u8981\u8BBE\u6210DEBUG\uFF0C\u751F\u4EA7\u73AF\u5883\u8BBE\u4E3AINFO\u6216ERROR
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3、配置web.xml
Spring框架和Spring MVC框架均需要在web.xml中进行配置。web.xml的初始内容为空,具体配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
Spring框架为了能够在应用加载的时候能够获取Spring容器,因此需要在web.xml中注册ServletContext监听器,并且需要在web.xml中指定Spring的Java Config配置类的路径。具体配置如下所示:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加载Java Config配置类-->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ccff.ssm.config.ApplicationConfig</param-value>
</context-param>
<!-- 注册ServletContext监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>
在上面配置context-param中,对于名为“contextConfigLocation”的值“com.ccff.ssm.config.ApplicationConfig”,我们目前还没有创建,我们将在本篇博客的第4小节进行详细说明。
Spring MVC需要在web.xml中注册Spring MVC的中央调度器DispatcherServlet,而Spring同样需要将自己的容器Java Config配置类作为初始化参数配置到DispatcherServlet中。具体配置如下所示:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加载Java Config配置类-->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ccff.ssm.config.ApplicationConfig</param-value>
</context-param>
<!-- 注册ServletContext监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--注册Spring MVC的中央调度器DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ccff.ssm.config.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
最后,为了避免整个应用出现中文乱码问题,因此需要在web.xml中注册字符编码过滤器。具体配置如下所示:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加载Java Config配置类-->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ccff.ssm.config.ApplicationConfig</param-value>
</context-param>
<!-- 注册ServletContext监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--注册Spring MVC的中央调度器DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>