浅谈SpringMVC过滤器CharacterEncodingFilter和HiddenHttpMethodFilter
1、解决乱码CharacterEncodingFilter
在Web.xml
<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>
采用HiddentHttpMethodFilter过滤器的方式实现put、delete
第一步:web.xml
<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>
第二步:jsp
<h1>HiddenHttpMethodFilter过滤器</h1>
<form action="params/testHttpMethodFilter" method="post">
用户名称:<input type="test" name="username"/>
<input type="hidden" name="_method" value="PUT"/>
<input type="submit"/>
</form>
第三步:controller
@RequestMapping(value="/testHttpMethodFilter",method = RequestMethod.PUT)
@ResponseBody
public String testHttpMethodFilter(){
System.out.println("testHttpMethodFilter.put");
return "success7676766767";
}