获取参数编码

发布于 2024-10-27 08:21:57 字数 323 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2024-11-03 08:21:57

您遇到此问题是因为请求区分了正文编码和 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

榕城若虚 2024-11-03 08:21:57

这又如何呢?有帮助吗?

在您的 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

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;
    }

}

What about this? Could it help?

In your 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:

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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文