POST参数在JSF 1.2中使用错误的编码 [英] POST parameters using wrong encoding in JSF 1.2

查看:154
本文介绍了POST参数在JSF 1.2中使用错误的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web应用程序(JSF 1.2,Spring和Tomcat 7)中的字符集编码有问题,我已经用尽了测试的想法,看看它出了问题。



每当我提交çã时,我得到çã:这意味着我的数据POSTed为 UTF-8 正在转换为ISO- 8859-1在JSF生命周期的某个地方。



我知道错误的转换是UTF-8到ISO-8859-1,因为它是相同的输出: p>

  System.out.println(new String(çã.getBytes(UTF-8),ISO-8859-1 )); 

我相信错误的转换是在JSF生命周期的某个地方我在我的MB中设置验证器:

  public void debugValidator(FacesContext context,UIComponent component,
Object object) throws ValidationException {
System.out.println(debug validator:);
System.out.println(object);
System.out.println(\\\
);
throw new ValidationException(DEBUG:+ object.toString());
}

,其消息返回如下:DEBUG:çã p>


  • 我在所有的.xhtml页面中第一行为<?xml version =1.0encoding = UTF-8?>

  • 我使用Facelets,根据 BalusC的文章默认使用UTF-8

  • 我需要,但我仍然设置,春天的 CharacterEncodingFilter 在我的 web.xml 将请求字符编码设置为UTF-8。

  • 我在Tomcat的 server.xml 文件中插入 URIEncoding =UTF-8只是为了保证

  • 这不是我的浏览器的错误,它在控制台中打印相同的东西,我的环境都是 UTF-8


你有什么想法,我可以测试什么?可能是我错误的假设?



提前感谢!

解决方案

BalusC的答案帮助我更好地理解这个问题,但是对我来说,解决方法是将字符编码过滤器设置为链中的第一个过滤器(将其置于 web .xml 文件)。



这是我使用的过滤器:

 code><! -  filter enforcing charset UTF-8  - 必须先在链中过滤! - > 
< filter>
< filter-name> characterEncodingFilter< / 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>
< init-param>
< param-name> forceEncoding< / param-name>
< param-value> true< / param-value>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> characterEncodingFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

显然,在过滤器设置参数之前读取了数据。
我从此页获得提示: http://tech.top21.de/techblog/20100421-solving-problems-with-request-parameter-encoding.html



感谢大家!


I'm having a problem with charset encoding in my web application (JSF 1.2, Spring and Tomcat 7), and I've ran out of ideas of what to test to see where it is going wrong.

Whenever I submit something like 'çã' I get 'çã': that means my data POSTed as UTF-8 is being converted to ISO-8859-1 somewhere in the JSF life cycle.

I know that the wrong conversion is UTF-8 to ISO-8859-1 cause it's the same output for:

System.out.println(new String("çã".getBytes("UTF-8"), "ISO-8859-1"));

I believe that the wrong conversion is somewhere in the JSF life cycle (can it be before?) cause I set up a validator in my MB:

public void debugValidator(FacesContext context, UIComponent component,
        Object object) throws ValidationException {
    System.out.println("debug validator:");
    System.out.println(object);
    System.out.println("\n");
    throw new ValidationException("DEBUG: " + object.toString());
}

and its message returns as: "DEBUG: çã"

  • I have in all my .xhtml pages the first line as <?xml version="1.0" encoding="UTF-8"?>.
  • I'm using Facelets, which according to BalusC's article uses UTF-8 by default
  • So it wouldn't need but I set up anyway, Spring's CharacterEncodingFilter in my web.xml to set the request character encoding to UTF-8.
  • I put URIEncoding="UTF-8" in Tomcat's server.xml file, just to guarantee
  • It is not my browser's fault, it prints the same thing in the console, and my environment is all UTF-8.

Do you have any idea of what more can I test? What could be my wrong assumption?

Thanks in advance!

解决方案

BalusC's answer helped me to better understand the problem, but what solved it for me was putting the Character Encoding Filter as the FIRST filter in the chain (putting it above all the others in the web.xml file).

This is the filter I used:

<!-- filter enforcing charset UTF-8 - must be first filter in the chain! -->
<filter>
    <filter-name>characterEncodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Apparently, the data was read before the parameter was set by the filter. I got the hint from this page: http://tech.top21.de/techblog/20100421-solving-problems-with-request-parameter-encoding.html

Thanks everybody!

这篇关于POST参数在JSF 1.2中使用错误的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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