在webxml 中用spring提供的过滤器
<filter> <filter-name>encoding</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>
以下是类的源码
public class CharacterEncodingFilter extends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; public void setEncoding(String encoding) { this.encoding = encoding; } public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { //为什么 request.getCharacterEncoding() 返回值会是null if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); }
以下是调用方法的源码
@Override public String getCharacterEncoding() { return super.getCharacterEncoding() != null ? super.getCharacterEncoding() : WebUtils.DEFAULT_CHARACTER_ENCODING; }
按照这个逻辑,怎么方法的返回值也不是null啊!!
Tobey_滔
相关分类