ITeye博客

Spring 字符过滤器解决乱码

peryou · 2013-01-21 18:11 · java新手笔记心得 · 企业架构 · Markdown

当你用尽了网上的方法还是解决不了乱码问题,或是嫌网上的解决方法太麻烦时,你想到了过滤器,当您辛辛苦苦写了一个过滤器,或是用了Spring的过滤器还是没解决问题时,那么你就会很恼火,我最近就遇上这么一个问题,用尽了网上的方法,spring设置的过滤编码也是UTF-8,但就是不起作用,如下:

 <filter>
        <filter-name>SetCharacterEncoding</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

 

到后来发现,

<filter-mapping>
        <filter-name>SetCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

的位置放错了,放到了WEB.XML 的最后,所以我设置的过滤器根本就没起到作用,改变了一下位置

把

<filter-mapping>
        <filter-name>SetCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>紧紧的放在

 

<filter>
        <filter-name>SetCharacterEncoding</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>的后面,我的乱码问题终于解决了。

 

总结:web.xml中过滤器的设置,<filter-mapping>
最好紧紧的放在<filter>的后面,如下:

<filter>
        <filter-name>SetCharacterEncoding</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

<filter-mapping>
        <filter-name>SetCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

这样或许就能解决你的问题。

评论(7)

  1. #7 楼 · jinnianshilongnian · 2013-01-22 07:56

    peryou writes:

    [quote=jinnianshilongnian][quote=peryou][quote=jinnianshilongnian]其实放在最前边的原因是, request.setCharacterEncoding必须在通过request.getParameter之前调用,否则无效。[/quote] 嗯 您说的这个我知道,但是我每次在调用request.getParameter("*")前加上this.getRequest().setCharacterEncoding("UTF-8");还是不起作用呢? 比如这样: action中有如下代码: this.getRequest().setCharacterEncoding("UTF-8"); String dwid = this.getRequest().getParameter("dwmc"); 得到的还是乱码。[/quote] 嗯,这是我之前总结的web应用中文乱码解决方案;(如果你是GET请求默认.setCharacterEncoding是不起作用的) [url]http://www.iteye.com/topic/1121242#2322208[/url][/quote]

    嗯谢谢您的回答,看了您的博客感谢,我的是POST请求,tomcat中也配置了,页面中也设置了,java代码中也设置了,但就是不起作用,所以很恼火呵呵,但我的问题最后解决确实是因为过滤器设置的不对。

    嗯 以我目前看到的就是顺序问题造成的 也就是刚开始说的

  2. #6 楼 · peryou · 2013-01-22 07:54

    jinnianshilongnian writes:

    [quote=peryou][quote=jinnianshilongnian]其实放在最前边的原因是, request.setCharacterEncoding必须在通过request.getParameter之前调用,否则无效。[/quote] 嗯 您说的这个我知道,但是我每次在调用request.getParameter("*")前加上this.getRequest().setCharacterEncoding("UTF-8");还是不起作用呢? 比如这样: action中有如下代码: this.getRequest().setCharacterEncoding("UTF-8"); String dwid = this.getRequest().getParameter("dwmc"); 得到的还是乱码。[/quote] 嗯,这是我之前总结的web应用中文乱码解决方案;(如果你是GET请求默认.setCharacterEncoding是不起作用的) [url]http://www.iteye.com/topic/1121242#2322208[/url]

    嗯谢谢您的回答,看了您的博客感谢,我的是POST请求,tomcat中也配置了,页面中也设置了,java代码中也设置了,但就是不起作用,所以很恼火呵呵,但我的问题最后解决确实是因为过滤器设置的不对。

  3. #5 楼 · jinnianshilongnian · 2013-01-22 07:22

    peryou writes:

    [quote=jinnianshilongnian]其实放在最前边的原因是, request.setCharacterEncoding必须在通过request.getParameter之前调用,否则无效。[/quote] 嗯 您说的这个我知道,但是我每次在调用request.getParameter("*")前加上this.getRequest().setCharacterEncoding("UTF-8");还是不起作用呢? 比如这样: action中有如下代码: this.getRequest().setCharacterEncoding("UTF-8"); String dwid = this.getRequest().getParameter("dwmc"); 得到的还是乱码。

    嗯,这是我之前总结的web应用中文乱码解决方案;(如果你是GET请求默认.setCharacterEncoding是不起作用的) http://www.iteye.com/topic/1121242#2322208

  4. #4 楼 · masuweng · 2013-01-21 21:48

    这个问题早些时候就遇到过,因为web.xml中初始化spring容器时,则会设计字符编码类型的

  5. #3 楼 · masuweng · 2013-01-21 21:45

    这个问题早些时候就遇到过了,

  6. #2 楼 · peryou · 2013-01-21 19:03

    jinnianshilongnian writes:

    其实放在最前边的原因是, request.setCharacterEncoding必须在通过request.getParameter之前调用,否则无效。

    嗯 您说的这个我知道,但是我每次在调用request.getParameter("*")前加上this.getRequest().setCharacterEncoding("UTF-8");还是不起作用呢? 比如这样: action中有如下代码: this.getRequest().setCharacterEncoding("UTF-8"); String dwid = this.getRequest().getParameter("dwmc"); 得到的还是乱码。

  7. #1 楼 · jinnianshilongnian · 2013-01-21 18:35

    其实放在最前边的原因是, request.setCharacterEncoding必须在通过request.getParameter之前调用,否则无效。

评论功能维护中,当前仅展示历史评论。