1、配置Spring相关配置,启动Spring的容器<!-- 启动Spring的容器 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
2、新建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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Spring的配置文件,主要配置和业务逻辑有关的 --> </beans>
3、在web.xml中配置springmvc的前段控制器,拦截所有的请求<ser箪滹埘麽vlet> <servlet-name&爿讥旌护gt;dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
4、建立一个Springmvc的配置文件:dispatcherServlet-servlet.xml
5、配置SpringMVC字符编码过滤器:<filter> <filter-name>characterEn艘早祓胂codingFilter</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>
6、配置Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求<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>
7、注意,过滤器是有先后的,一定要把字符编码过滤器放在最前面。