web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 1.前端控制器:接收所有的请求并分发出去,加载springmvc.xml -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在DispatcherServlet初始化时自动解析init-param标签 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服务器已启动就实例化DispatcherServlet,紧跟着要初始化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 2.过滤器:解决post请求的中文参数乱码问题
过滤器比Servlet先执行,默认就是在服务器启动的时候就实例化了
-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 指定字符集:初始化的时候会解析encoding -->
<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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 1.静态资源放行:默认的Servlet处理静态资源。因为我们写的前端控制器匹配方式是/, 这个/会替换tomcat中default-servlet的配置。原来default-servlet处理静态资源的 -->
<mvc:default-servlet-handler />
<!-- 2.扫描包路径:Spring容器回去扫描cn.itsource及其子包种的所有类,如果发现有 @Controller,@Service,@Repository,@Com......注解就创建对象 -->
<context:component-scan base-package="com.knd" />
<!-- 3.开启Spring对Mvc的支持:能够使用@RequetMapping注解 -->
<mvc:annotation-driven>
<!-- 避免在IE浏览器中返回JSON时出现下载文件的情况 -->
<mvc:message-converters>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 4.视图解析器:解析视图 -->
<!-- 设置视图路径的前后缀,该配置可以让我们写视图路径的时候更简单,只写视图名 -->
<!-- 只要配置了视图解析器,不管方法返回任何值,都要经过视图解析器:除了显式转发和重定向 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 5.文件上传解析器:将复杂的表单数据解析成MultipartFile对象:
坑:id属性值不能随便写,必须写multipartResolver
-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 最大上传大小 -->
<property name="maxUploadSize" value="20000000" />
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 配置拦截器组 -->
<mvc:interceptors>
<!-- 拦截器 -->
<mvc:interceptor>
<!-- 要拦截的配置,该配置必须写在不拦截的上面,/*拦截一级请求,/**拦截多级请求 -->
<mvc:mapping path="/**" />
<!-- 设置不拦截的配置 -->
<mvc:exclude-mapping path="/json/list"/>
<!-- 配置拦截器 -->
<bean class="com.knd.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>