springmvc+spring+mybatis整合
1. 先搭建整合的环境 2. 先把Spring的配置搭建完成 3. 再使用Spring整合SpringMVC框架 4. 最后使用Spring整合MyBatis框架
- 创建工程,导入SSM整合所需要的所有依赖jar包
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.0.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <mysql.version>5.1.6</mysql.version> <mybatis.version>3.4.5</mybatis.version> </properties> <dependencies> <!-- spring --> <dependency> <!-- spring中的el表达式的解析依赖 --> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- jsp中的el表达式需要的解析依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies>
- 完善maven工程的目录,java+resources包
- 创建domain实体类
package com.itlearn.domain; import java.io.Serializable; public class Account implements Serializable { private Integer ID; private Integer UID; private Double MONEY; ... }
- 编写Dao接口:(dao不需要实现类,只写接口,在接口上配置注解)
package com.itlearn.dao; import com.itlearn.domain.Account; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository("accountDao") public interface IAccountDao { public List<Account> findAll(); public void saveAccount(Account account); }
- 编写service接口和实现类
package com.itlearn.service; import com.itlearn.domain.Account; import java.util.List; public interface IAccountService { //查询所有账户 public List<Account> findAll(); //保存账户 public void saveAccount(Account account); }
package com.itlearn.service.impl; import com.itlearn.dao.IAccountDao; import com.itlearn.domain.Account; import com.itlearn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("accountService") public class IAccountServiceImpl implements IAccountService { /** * 业务层查询所有账户 * @return */ @Override public List<Account> findAll() { List<Account> accounts = dao.findAll(); return accounts; } /** * 业务层保存账户 * @param account */ @Override public void saveAccount(Account account) { dao.saveAccount(account); } }
- 在ssm整合项目中创建applicationContext.xml的配置文件,编写具体的配置信息
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解的扫描:只扫描处理service层和dao层的注解,而不负责web层的,因为web层的注解要交给springmvc负责 --> <context:component-scan base-package="com.itlearn"> <!-- 配置Controller注解不扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> </beans>
- 在test包中 创建service层的测试方法,验证spring是否搭建成功
package com.itlearn.test; import com.itlearn.service.IAccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void testService(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取对象 IAccountService service = context.getBean("accountService",IAccountService.class); //调用方法 service.findAll(); } } //测试结果:spring环境搭建成功
- 在web.xml中配置DispatcherServlet前端控制器
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置加载springmvc.xml配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置启动服务器就创建该ispatcherServlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置解决中文乱码的过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 创建springmvc.xml的配置文件,编写配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置开启注解扫描,只扫描Controller注解 --> <context:component-scan base-package="com.itlearn"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter> </context:component-scan> <!-- 配置视图解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 设置静态资源不过滤 --> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 配置开启springmvc注解的支持 --> <mvc:annotation-driven/> </beans>
- 创建Controller包以及controller类:AccountController.java
package com.itlearn.controller; import com.itlearn.domain.Account; import com.itlearn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; /** * web层控制器 */ @Controller("accountController") @RequestMapping("/account") public class AccountController { @Autowired private IAccountService service; @RequestMapping("/findAll") public String findAll(){ System.out.println("表现层:查询所有账户..."); return "success"; } }
- 在index.jsp页面中编写超链接,访问controller方法
<a href="/account/findAll">FindAll_Controller</a>
- 测试结果:环境搭建成功
目的:在controller中能成功的调用service对象中的方法
在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener 监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件,所以需要手动 在web.xml中配置applicationContext.xml的位置,作为监听器的参数)
<!-- 配置spring的监听器:该监听器只能加载WEB-INF目录下的applicationContext.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>
- 在controller中注入service对象,调用service对象的方法进行测试
package com.itlearn.controller; import com.itlearn.domain.Account; import com.itlearn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; /** * web层控制器 */ @Controller("accountController") @RequestMapping("/account") public class AccountController { //注入service @Autowired private IAccountService service; @RequestMapping("/findAll") public String findAll(Model model){ System.out.println("表现层:查询所有账户..."); return "success"; } }
- 在web项目中编写SqlMapConfifig.xml的配置文件,编写核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbcConfig.properties"></properties> <!-- 开启允许延迟加载 在开启之后,所有的sql语句,只要满足延迟加载的,都会进行延迟加载 如果:在一对一,多对一的情况下,想要不使用延迟加载,就要通过:fetchtype属性进行配置立即加载 fetchtype = "eager":立即加载 --> <settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> <!-- 为domain下实体类配置别名;不区分大小写 --> <typeAliases> <package name="com.itlearn.domain"></package> </typeAliases> <!-- 配置环境 --> <environments default="mysql"> <!-- 配置mysql的环境 --> <environment id="mysql"> <!-- 配置事务的类型 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源(连接池) --> <dataSource type="POOLED"> <!-- 配置连接事务的四个基本信息 --> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <package name="com.itlearn.dao"></package </mappers> </configuration>
- 引入jdbcConfig.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatisdb jdbc.username=root jdbc.password=root
- 在IAccountDao接口的方法上添加注解,编写SQL语句
package com.itlearn.dao; import com.itlearn.domain.Account; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository("accountDao") public interface IAccountDao { //查询所有账户 @Select("select * from account") public List<Account> findAll(); //保存账户 @Insert("insert into account values(#{ID},#{UID},#{MONEY})") public void saveAccount(Account account); }
- 编写测试的方法
public class Demo1 { @Test public void run1() throws Exception { // 加载配置文件 InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); // 创建工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); // 创建sqlSession对象 SqlSession session = factory.openSession(); // 获取代理对象 AccountDao dao = session.getMapper(AccountDao.class); // 调用查询的方法 List<Account> list = dao.findAll(); for (Account account : list) { System.out.println(account); } // 释放资源 session.close(); inputStream.close(); }
- 目的:把SqlMapConfifig.xml配置文件中的内容配置到applicationContext.xml配置文件中
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解的扫描:只扫描处理service层和dao层的注解,而不负责web层的,因为web层的注解要交给springmvc负责 --> <context:component-scan base-package="com.itlearn"> <!-- 配置Controller注解不扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> <!-- 将Mybatis配置文件SqlMapConfig.xml配置文件整合到此 --> <!-- 引入properties文件 --> <context:property-placeholder location="classpath:jdbcConfig.properties"/> <!-- 配置C3P0的连接池对象 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 配置连接事务的四个基本信息 --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSession的工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置扫描dao的包 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itlearn.dao"/> </bean> </beans>
- 在service中注入dao对象,进行测试 (在此SqlMapConfig.xml可以删除了)
package com.itlearn.service.impl; import com.itlearn.dao.IAccountDao; import com.itlearn.domain.Account; import com.itlearn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("accountService") public class IAccountServiceImpl implements IAccountService { //注入:dao对象 @Autowired private IAccountDao dao; /** * 业务层查询所有账户 * @return */ @Override public List<Account> findAll() { List<Account> accounts = dao.findAll(); return accounts; } /** * 业务层保存账户 * @param account */ @Override public void saveAccount(Account account) { dao.saveAccount(account); } }
- 编写测试方法:Controller调用service,service调用dao实现查询所有操作
package com.itlearn.controller; import com.itlearn.domain.Account; import com.itlearn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; /** * web层控制器 */ @Controller("accountController") @RequestMapping("/account") public class AccountController { @Autowired private IAccountService service; @RequestMapping("/findAll") public String findAll(Model model){ List<Account> accounts = service.findAll(); model.addAttribute("accounts",accounts); return "list"; } }
index.jsp页面
<a href="/account/findAll">FindAll_Controller</a>
list.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <c:forEach items="${accounts}" var="account"> ${account.MONEY} </c:forEach> </body>
结论:可以实现查询所有操作
- 在applicationContext.xml中配置Spring的声明式事务管理,以实现保存账户操作(即:实现增删改操作,因为这三个都有事务)
<!-- 配置spring声明式事务管理 --> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!-- 配置AOP增强 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itlearn.service.impl.*ServiceImpl.*(..))"/> </aop:config>
- 测试保存操作
@RequestMapping("/save") public void saveAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException { service.saveAccount(account); response.sendRedirect(request.getContextPath()+"/account/findAll"); return; }
index.jsp页面
<form action="/account/save" method="post"> ID:<input type="text" name="ID"> UID:<input type="text" name="UID"> MONEY:<input type="text" name="MONEY"> <input type="submit" value="添加"> </form>
- applicationContext.xml完整配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解的扫描:只扫描处理service层和dao层的注解,而不负责web层的,因为web层的注解要交给springmvc负责 --> <context:component-scan base-package="com.itlearn"> <!-- 配置Controller注解不扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> <!-- 引入properties文件 --> <context:property-placeholder location="classpath:jdbcConfig.properties"/> <!-- 配置C3P0的连接池对象 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 配置连接事务的四个基本信息 --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSession的工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置扫描dao的包 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itlearn.dao"/> </bean> <!-- 配置spring声明式事务管理 --> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!-- 配置AOP增强 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itlearn.service.impl.*ServiceImpl.*(..))"/> </aop:config> </beans>
- springmvc.xml完整配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置开启注解扫描,只扫描Controller注解 --> <context:component-scan base-package="com.itlearn"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter> </context:component-scan> <!-- 配置视图解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 设置静态资源不过滤 --> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 配置开启springmvc注解的支持 --> <mvc:annotation-driven/> </beans>
- web.xml完整配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置加载springmvc.xml配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置启动服务器就创建该ispatcherServlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置解决中文乱码的过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置spring的监听器:该监听器只能加载WEB-INF目录下的applicationContext.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> </web-app>
原文:https://blog.51cto.com/14982816/2622778