MediaTypes如果这个类仅仅是为了加上charset=UTF-8​,那么这个类似乎是多余了

brett_chs 发布于 2014/11/07 11:23
阅读 193
收藏 0

@江南白衣 你好,想跟你请教个问题:

org.springside.modules.web.MediaTypes如果这个类仅仅是为了加上charset=UTF-8,那么这个类似乎是多余了,下面是Servlet2.4 API描述:

javax.servlet.ServletResponse
public abstract void setCharacterEncoding(java.lang.String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType or setLocale, this method overrides it. Calling setContentType with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

可以看出,在web.xml中配置了CharacterEncodingFilter且forceEncoding=true的情况下,ContentType是否加上charset=UTF-8并不重要,因为会被response.setCharacterEncoding(this.encoding);覆盖。

<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



加载中
返回顶部
顶部