spring 获取参数编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5445990/
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-09-08 04:24:33  来源:igfitidea点击:

Get Parameter Encoding

springspring-mvcjakarta-ee

提问by Erik

I have a problem using spring mvc and special chars in a GET request. Consider the following method:

我在 GET 请求中使用 spring mvc 和特殊字符时遇到问题。考虑以下方法:

@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.

我向其发送名称包含“?”的 GET 请求。(德语变音),例如。这导致 spring 收到“?¤”,因为浏览器映射“?” 到%C3%A4

So, how can I get the correct encoded string my controller?

那么,我怎样才能获得正确的编码字符串我的控制器呢?

Thanks for your help!

谢谢你的帮助!

采纳答案by Rihards

What about this? Could it help?

那这个呢?有帮助吗?

In your web.xml:

在您的web.xml 中

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.example.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <servlet-name>dispatcher</servlet-name>
    </filter-mapping>

com.example.CharacterEncodingFilter:

com.example.CharacterEncodingFilter

public class CharacterEncodingFilter implements Filter {

    protected String encoding;

    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding(encoding);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        encoding = null;
    }

}

回答by vicox

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.

您遇到了这个问题,因为请求区分了正文编码和 URI 编码。CharacterEncodingFilter 设置正文编码,但不设置 URI 编码。

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

您需要在 Tomcat server.xml 的所有连接器中将URIEncoding="UTF-8" 设置为属性。见这里:http: //tomcat.apache.org/tomcat-6.0-doc/config/ajp.html

Or, alternatively, you can set useBodyEncodingForURI="True".

或者,您也可以设置 useBodyEncodingForURI="True"。

If you're using the maven tomcat plugin, just add this parameter:

如果您使用的是 maven tomcat 插件,只需添加此参数:

mvn -Dmaven.tomcat.uriEncoding=UTF-8tomcat:run

mvn -Dmaven.tomcat.uriEncoding=UTF-8tomcat:run