如果spring的配置文件和web.xml不在同一个文件夹
在web.xml中分发器中写一个初始化参数
<init-param>
<param-name>contextConfigLocation</param-name>
<!--可以放在任何地方 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
@contorller return可以重定向
return “redirect:/*/*”
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" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置扫描注解-->
<context:annotation-config/>
<!--告诉spring,要扫描com.tledu包下面的注解-->
<context:component-scan base-package="com.example.ssm"/>
<!--加上aop的约束-->
<aop:aspectj-autoproxy/>
<!--使用注解的方式进行MVC映射 -->
<mvc:annotation-driven >
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--让spring去管理mybatis-->
<!--需要把mybatis所需要的依赖,放到spring容器中-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2) 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--最大连接数-->
<property name="maxActive" value="${jdbc.max}"/>
<!--初始空闲池的个数-->
<property name="minIdle" value="${jdbc.min}"/>
</bean>
<!-- 3) 获取 SqlSessionFactory 工厂类-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 4) 搜索有哪些 mapper 实现类,把mapper接口自动配置成 spring 中的 <bean>-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- name="basePackage":(起始)包名, 从这个包开始扫描-->
<property name="basePackage" value="com.example.ssm.mapper"/>
</bean>
<!-- 使使用注解配置的事务行为生效 -->
<tx:annotation-driven transaction-manager="txManager"/><!-- 仍然需要一个PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (这个需要的对象是在其他地方定义的) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<!-- 以下配置中,必须按顺序配置:mapping > exclude-mapping > bean -->
<!-- 1.mapping:拦截路径 , 可以有多个mapping节点 -->
<mvc:mapping path="/**"/>
<mvc:mapping path="/user/**"/>
<!-- 不再拦截路径中的请求,完全不受理 -->
<!-- 2.exclude-mapping:白名单,可以配置多个 -->
<mvc:exclude-mapping path="/user/login" />
<mvc:exclude-mapping path="/login" />
<mvc:exclude-mapping path="/l" />
<!-- 3.bean:配置拦截器类,只配置class即可 -->
<bean class="com.example.ssm.interceptor.AuthInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--单位b-->
<property name="maxUploadSize" value="5000000000"/>
</bean>
</beans>
mybatis-config.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>
<typeAliases>
<package name="com.example.ssm.entity"/>
</typeAliases>
<mappers>
<package name="com.example.ssm.mapper"/>
</mappers>
</configuration>
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">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>/WEB-INF/jsp/user/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>basic</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--可以放在任何地方 -->
<!--<param-value>/a/b/c/d/xxxxxxxxx.xml</param-value> -->
<!--
由于我们的配置文件在resource目录下,所以需要如下配置
-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--把所有的请求交给了spring mvc进行处理-->
<servlet-mapping>
<servlet-name>basic</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>
jdbc.properties 略