配置文件
1、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">
<!--加载顺序:1、context-param 2、listener 3、filter 4、servlet-->
<!--自定义监听器-->
<listener>
<listener-class>com.jason.lee.mvc.listener.SpringListener</listener-class>
</listener>
<!--Spring提供的监听器 【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:spring.xml</param-value>
</context-param>
<!--字符编码过滤器-->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--转换请求方式过滤器-->
<!--客户端为POST请求,且_method参数有值才进行转换-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器:加载SpringMVC配置文件 【默认在WEB-INF下文件为 <servlet-name>-servlet.xml 】-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--自定义设置SpringMVC的配置文件位置-->
<!--<init-param>-->
<!--<param-name>contextConfigLocation</param-name>-->
<!-- 此处指向的的是SpringMVC的配置文件 -->
<!--<param-value>classpath:xxx.xml</param-value>-->
<!--</init-param>-->
<!--设置servlet的加载时间
默认在第一次访问时加载
设置此标签会将servlet的加载时间提前到项目启动时
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、spring.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"
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">
<context:component-scan base-package="com.jason.lee.mvc"/>
</beans>
3、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:context="http://www.springframework.org/schema/context"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.jason.lee.mvc.controller"/>
<!-- 对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
如果不是静态资源的请求,才由DispatcherServlet继续处理。-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件解析器:将上传的File转换为MultipartFile-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxInMemorySize" value="100000"/>
</bean>
<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--将不同的异常映射到不同的jsp页面-->
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">error</prop>
</props>
</property>
</bean>
<mvc:interceptors>
<!--默认拦截所有请求-->
<!--配置拦截器一:全限定名-->
<bean class="com.jason.lee.mvc.interceptor.FirstInterceptor"/>
<bean class="com.jason.lee.mvc.interceptor.SecondInterceptor"/>
<!--配置拦截器二:拦截器上注解@Component-->
<!-- <ref bean="firstInterceptor"/>-->
<!--设置自定义拦截-->
<!-- <mvc:interceptor>-->
<!-- <mvc:mapping path="/testGet"/>-->
<!-- <ref bean="firstInterceptor"/>-->
<!-- </mvc:interceptor>-->
</mvc:interceptors>
<!--MVC注解驱动-->
<mvc:annotation-driven/>
</beans>