简体   繁体   English

Spring MVC和UTF-8:如何使用瑞典特殊字符?

[英]Spring MVC and UTF-8: How to work with Swedish special characters?

I try to find the word with special Swedish characters "bäck" in my database, I have a jsp-page: 我尝试在数据库中找到带有特殊瑞典语字符“bäck”的单词,我有一个jsp页面:

<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
    ...
<form name="mainform" action="/web/admin/users/">
    <input id="keywords" type="text" name="keywords" size="30"
           value="${status.value}" tabindex="1" />
    <button class="link" type="submit">Search</button>
</form>

a filter: 过滤器:

public class RequestResponseCharacterEncodingFilter extends OncePerRequestFilter {

    private String encoding;

    private boolean forceEncoding;

    protected void doFilterInternal(
            HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
       request.setCharacterEncoding(this.encoding);
       response.setCharacterEncoding(this.encoding);
       filterChain.doFilter(request, response);
    }
}

web.xml web.xml

<web-app ...>
...
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>test.testdomain.spring.RequestResponseCharacterEncodingFilter</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>
...
</web-app>

When I start finding the "bäck" word, It appears like this bäck . 当我开始寻找“贝克”一词时,它看起来像是bäck A request is encoded into UTF-8: 一个请求被编码为UTF-8: IE请求捕获

but right before I exit my doFilterInternal method in my filter in debugger I see: 但是就在我在调试器的过滤器中退出doFilterInternal方法之前,我看到: IDEA调试器

What I am doing wrong? 我做错了什么? Why is the text not encoded into UTF-8? 为什么文本未编码为UTF-8?

EDIT: It is very strange, I've just tried to query in Chrome and Mozilla Firefox and there it works well, so it appears to me that I have this problem only in Internet Explorer 编辑:这是很奇怪,我只是试图在Chrome和Mozilla Firefox中查询,并且在那里工作良好,所以在我看来,我只有在Internet Explorer中才有此问题

EDIT : Internet Explorer gives me this string: b%C3%A4ck but Mozilla Firefox and Chrome give me the string: b%E4ck . 编辑 :Internet Explorer给我这个字符串: b%C3%A4ck但Mozilla Firefox和Chrome给我字符串: b%E4ck They are obviously different why is that? 他们显然是不同的,为什么呢?

Your screenshots indicate that your search keyword, bäck, is sent as part of the URL, as a URL parameter. 屏幕快照表明您的搜索关键字bäck作为URL的一部分作为URL参数发送。 It also indicates that this work seems correctly UTF-8 URL encoded. 这也表明这项工作似乎已正确地以UTF-8 URL编码。 And the String you get back in your debugger is typical of ISO-Latin decoding of UTF-8 encoded bytes : eg the HTTPServletRequest parser used ISO-Latin parsing for a UTF-8 encoded string. 而且您在调试器中返回的String是UTF-8编码字节的ISO-Latin解码的典型代表:例如,HTTPServletRequest解析器对UTF-8编码的字符串使用ISO-Latin解析。

So, your ServletFilter is of no help in interpreting it : 因此,您的ServletFilter对解释它没有帮助:

request.setCharacterEncoding(this.encoding);
response.setCharacterEncoding(this.encoding);

Because as the javadoc says : these methods work on the body of HTTP request, not on its URLs. 因为正如javadoc所说:这些方法适用于HTTP请求的主体,而不适用于其URL。

/**
 * Overrides the name of the character encoding used in the body of this
 * request. This method must be called prior to reading request parameters
 * or reading input using getReader(). Otherwise, it has no effect.
 * 

Seeing URL parameter parsing is a responsability of your Servlet container, the setting you should look at probably is a container level one. 看到URL参数解析是Servlet容器的职责,您应该查看的设置可能是容器级别的。 For example, on Tomcat, as stated in the documentation at : http://tomcat.apache.org/tomcat-7.0-doc/config/http.html : 例如,在Tomcat上,如文档所述: http : //tomcat.apache.org/tomcat-7.0-doc/config/http.html

URIEncoding : This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. URIEncoding:这指定%xx解码URL后用于解码URI字节的字符编码。 If not specified, ISO-8859-1 will be used. 如果未指定,将使用ISO-8859-1。

By default, it uses ISO-8859-1. 默认情况下,它使用ISO-8859-1。 You should change that to UTF-8, and then, your request parameters will be correctly parsed from your servlet container, and passed to the HTTPServletRequest object. 您应该将其更改为UTF-8,然后,将从您的servlet容器中正确解析您的请求参数,并将其传递给HTTPServletRequest对象。

EDIT : As you are seeing inconsistent browser behaviour, you may look into the consistency of your HTML form. 编辑:当您看到不一致的浏览器行为时,您可能会调查HTML表单的一致性。 Please make sure that 请确保

  1. Your HTTP Content-Type header AND your HTML "meta" tag defining the charset are both present and coherent in declaring a charset. HTTP Content-Type标头和定义字符集的HTML“ meta”标记在声明字符集时都存在且一致。 (Given your servlet filter, they both should be UTF-8) (鉴于您的servlet过滤器,它们都应为UTF-8)
  2. You actually respect that charset declaration in the body of your response (you actually write UTF-8 strings from your JSP - or whatever else) 您实际上在响应的主体中尊重了该charset声明(实际上是从JSP编写UTF-8字符串-或其他方式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM