获取参数编码
我在 GET 请求中使用 spring mvc 和特殊字符时遇到问题。考虑以下方法:
@RequestMapping("/update")
public Object testMethod(@RequestParam String name) throws IOException {
}
例如,我向该方法发送名称包含“ä”(德语元音变音)的 GET 请求。这会导致 spring 收到“ä”,因为浏览器将“ä”映射到 %C3%A4
。
那么,我怎样才能获得控制器的正确编码字符串呢?
感谢您的帮助!
I have a problem using spring mvc and special chars in a GET request. Consider the following method:
@RequestMapping("/update")
public Object testMethod(@RequestParam String name) throws IOException {
}
to which I send a GET request with name containing an "ä" (german umlaut), for instance. It results in spring receiving "ä" because the browser maps "ä" to %C3%A4
.
So, how can I get the correct encoded string my controller?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到此问题是因为请求区分了正文编码和 URI 编码。 CharacterEncodingFilter 设置正文编码,但不设置 URI 编码。
您需要将 URIEncoding="UTF-8" 设置为 Tomcat server.xml 中所有连接器的属性。请参阅此处: http://tomcat.apache.org/tomcat-6.0- doc/config/ajp.html
或者,您也可以设置 useBodyEncodingForURI="True"。
如果您使用的是maven tomcat插件,只需添加此参数:
mvn -Dmaven.tomcat.uriEncoding=UTF-8 tomcat:run
You're having this problem, because the request differentiates between body encoding and URI encoding. A CharacterEncodingFilter sets the body encoding, but not the URI encoding.
You need to set URIEncoding="UTF-8" as an attribute in all your connectors in your Tomcat server.xml. See here: http://tomcat.apache.org/tomcat-6.0-doc/config/ajp.html
Or, alternatively, you can set useBodyEncodingForURI="True".
If you're using the maven tomcat plugin, just add this parameter:
mvn -Dmaven.tomcat.uriEncoding=UTF-8 tomcat:run
这又如何呢?有帮助吗?
在您的 web.xml 中:
com.example.CharacterEncodingFilter:
What about this? Could it help?
In your web.xml:
com.example.CharacterEncodingFilter: