[英]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:
but right before I exit my doFilterInternal
method in my filter in debugger I see: 但是就在我在调试器的过滤器中退出
doFilterInternal
方法之前,我看到:
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
请确保
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.