在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啊!!
1 回答
Tobey_滔
TA贡献242条经验 获得超128个赞
首先你的过滤器有没有配置映射??如果没有请配置!!!其次是request的编码默认是null,但是最终按照ISO那个走的....而且我发现你的过滤器没有一个初始化的步骤啊,比如init函数,然后获取初始化参数的UTF-8,赋值给encoding才对吧..不然你的encoding一直都是null..这样会导致无法setCharaterEncoding()的触发的!!!建议多检查...我说的不到一定对啊!!只是我个人见解...
添加回答
举报
0/150
提交
取消