java Spring MVC:CharacterEncodingFilter;为什么只强制设置响应编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11685644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:03:02  来源:igfitidea点击:

Spring MVC: CharacterEncodingFilter; why only set response encoding by force?

javaspringcharacter-encoding

提问by Martin Becker

I was having a look at the CharacterEncodingFilter provided by Spring MVC. I was wondering why it was only possible to set the response encoding when the request encoding was forced to the given encoding? Why not be able to set a default response encoding if nothing is specified in the accept header fields? Or if no encoding was present in the request?

我正在查看 Spring MVC 提供的 CharacterEncodingFilter 。我想知道为什么只有在请求编码被强制为给定编码时才可以设置响应编码?如果在接受头字段中没有指定任何内容,为什么不能设置默认响应编码?或者如果请求中没有编码?

The code:

代码:

@Override
protected void doFilterInternal(
  HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  throws ServletException, IOException {

  if (this.encoding != null && (this.forceEncoding 
      || request.getCharacterEncoding() == null)) {

    request.setCharacterEncoding(this.encoding);
    if (this.forceEncoding) {
      response.setCharacterEncoding(this.encoding);
    }
  }
  filterChain.doFilter(request, response);
}

I found this as reference https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanelstating that the response encoding can only be set when the request encoding is forcibly set. Why?

我发现这是作为参考 https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel指出响应编码只能在请求时设置强制设置编码。为什么?

Thanks in advance, Martin

提前致谢,马丁

回答by Nandkumar Tekale

I can tell you what Juergen Hoeller says on link https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel,

我可以告诉你 Juergen Hoeller 在链接https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel上所说的话,

Add following filter in web.xml (Servlet 2.4+) to set encoding :

在 web.xml (Servlet 2.4+) 中添加以下过滤器来设置编码:

<filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>

EDIT :

编辑 :

CharacterEncodingFilter: Current browsers typically do not set a character encoding even if specified in the HTML page or form. The above filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case("forceEncoding"="true"). If we strictly want to encode characters, we set it forcibly.

CharacterEncodingFilter:当前浏览器通常不会设置字符编码,即使在 HTML 页面或表单中指定。如果请求尚未指定编码,则上述过滤器可以应用其编码,或者在任何情况下强制执行此过滤器的编码 ("forceEncoding"="true")。如果我们严格要编码字符,我们强制设置。

  1. why it was only possible to set the response encoding when the request encoding was forced to the given encoding?
  2. Why not be able to set a default response encoding if nothing is specified in the accept header fields? Or if no encoding was present in the request?
  1. 为什么只有在请求编码被强制为给定编码时才可以设置响应编码?
  2. 如果在接受头字段中没有指定任何内容,为什么不能设置默认响应编码?或者如果请求中没有编码?

I think Boris'slinkin comment will answer these questions.

我认为评论中的Boris's链接将回答这些问题。

回答by dannybastos

If nothing else works, you can create a filter ( bean ) in security-Context.xml and set forceEnconding=true;

如果没有其他工作,您可以在 security-Context.xml 中创建一个过滤器 ( bean ) 并设置 forceEnconding=true;

<bean id="characterEncodingFilter" class="org.springframework.web.filter.CharacterEncodingFilter">
    <property name="encoding" value="utf-8"></property>
    <property name="forceEncoding" value="true"></property>
</bean>

Don't forget to set the new custom-Filter:

不要忘记设置新的自定义过滤器:

<security:custom-filter ref="characterEncodingFilter" after="FIRST"/>