对于过滤器来说,就是在第一次请求前就要过滤条件,比如登录条件过滤,乱码过滤、HTTP请求过滤,但是对于这个只知道先过滤字符乱码然后其他,那么为什么要这么做呢!我们可以从源码中看到问题的实质
CharacterEncodingFilter(通过构造方法来设置传入的字符集)
public class CharacterEncodingFilter extends OncePerRequestFilter {
@Nullable
private String encoding;
private boolean forceRequestEncoding = false ;
private boolean forceResponseEncoding = false ;
public CharacterEncodingFilter ( ) {
}
public CharacterEncodingFilter ( String encoding) {
this ( encoding, false ) ;
}
public void setEncoding ( @Nullable String encoding) {
this . encoding = encoding;
}
@Override
protected void doFilterInternal (
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String encoding = getEncoding ( ) ;
if ( encoding != null) {
if ( isForceRequestEncoding ( ) || request. getCharacterEncoding ( ) == null) {
request. setCharacterEncoding ( encoding) ;
}
if ( isForceResponseEncoding ( ) ) {
response. setCharacterEncoding ( encoding) ;
}
}
filterChain. doFilter ( request, response) ;
}
HiddenHttpMethodFilter,这个一上来就要了一个method并没有设置字符集 什么的,如果这个在前,那么必定会发生乱码
@Override
protected void doFilterInternal ( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if ( "POST" . equals ( request. getMethod ( ) ) && request. getAttribute ( WebUtils. ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request. getParameter ( this . methodParam) ;
if ( StringUtils. hasLength ( paramValue) ) {
requestToUse = new HttpMethodRequestWrapper ( request, paramValue) ;
}
}
filterChain. doFilter ( requestToUse, response) ;
}
所以必须先写CharacterEncodingFilter再写HiddenHttpMethodFilter