spring mvc 內(nèi)部如果 request.getCharacterEncoding() 為 null 就默認(rèn)為 ISO-8859-1扎谎,因此編寫一個(gè)filter顯式設(shè)置請(qǐng)求編碼:
package com.xzp;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(filterName="SetCharacterEncodingFilter",urlPatterns="/*")
public class SetCharacterEncodingFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
// TODO Auto-generated method stub
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
filterChain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}