获取参数编码 [英] Get Parameter Encoding

查看:109
本文介绍了获取参数编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我向其发送名称中包含ä的GET请求(德国变音符号),例如。它导致弹簧接收¤,因为浏览器将ä映射到%C3%A4

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?

感谢您的帮助!

推荐答案

这个怎么样?它有帮助吗?

What about this? Could it help?

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

}

这篇关于获取参数编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆