在JSF 1.2中使用错误编码的POST参数

Eli*_*les 4 jsf facelets utf-8 character-encoding tomcat7

我在我的Web应用程序(JSF 1.2,Spring和Tomcat 7)中遇到charset编码问题,而且我已经用尽了测试内容以查看它出错的地方.

每当我提交类似'çã'的内容时,我会得到'çã':这意味着我在UTF-8上发布的数据在JSF生命周期的某个地方被转换为ISO-8859-1.

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

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

我相信错误的转换是在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("\n");
    throw new ValidationException("DEBUG: " + object.toString());
}
Run Code Online (Sandbox Code Playgroud)

并且它的消息返回:"DEBUG:çã"

  • 我在我的所有.xhtml页面中都有第一行<?xml version="1.0" encoding="UTF-8"?>.
  • 我正在使用Facelets,根据BalusC的文章默认使用UTF-8
  • 所以它不需要,但我设置无论如何,Spring CharacterEncodingFilter在我的web.xml中将请求字符编码设置为UTF-8.
  • 我放入URIEncoding="UTF-8"Tomcat的server.xml文件,只是为了保证
  • 这不是我的浏览器的错,它在控制台中打印相同的东西,我的环境都是UTF-8.

你知道我还能测试什么吗?可能是我的错误假设?

提前致谢!

Eli*_*les 7

BalusC的回答帮助我更好地理解了这个问题,但是为我解决的问题是将字符编码过滤器作为链中的FIRST过滤器(将其置于web.xml文件中的所有其他过滤器之上).

这是我使用的过滤器:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢大家!