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

发布于 2024-12-22 16:32:40 字数 1308 浏览 0 评论 0原文

我在 Web 应用程序(JSF 1.2、Spring 和 Tomcat 7)中遇到了字符集编码问题,并且我已经不知道要测试什么来查看哪里出了问题。

每当我提交类似 'çã' 的内容时,我都会得到 'çã£':这意味着我以 UTF-8 形式发布的数据正在 JSF 生命周期中的某个位置转换为 ISO-8859-1。

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

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("\n");
    throw new ValidationException("DEBUG: " + object.toString());
}

其消息返回为:“DEBUG: §â£”

  • 我在所有 .xhtml 页面中第一行为
  • 我正在使用 Facelets,根据 BalusC 的文章 默认使用 UTF-8
  • 所以不需要,但我还是设置了 Spring 的 CharacterEncodingFilter 在我的 web.xml 中设置请求字符编码为 UTF-8。
  • 我将 URIEncoding="UTF-8" 放在 Tomcat 的 server.xml 文件中,只是为了保证
  • 这不是我的浏览器的错误,它在控制台中打印相同的内容,并且我的环境都是UTF-8

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

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

宣告ˉ结束 2024-12-29 16:32:40

BalusC 的回答帮助我更好地理解了这个问题,但为我解决这个问题的方法是将字符编码过滤器作为链中的第一个过滤器(将其置于 web.xml 中的所有其他过滤器之上)。 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>

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

谢谢大家!

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!

何以笙箫默 2024-12-29 16:32:40

这些症状表明浏览器使用 ISO-8859-1 编码而不是 UTF-8 发送数据。这又意味着未使用正确的 charset 属性设置 HTTP 响应 Content-Type 标头。例如,在 Firebug 中,您可以按如下方式找到它:

在此处输入图像描述

您是对的,Facelets 使用 UTF-默认为 8。但是 Facelets 的早期版本并未被编程为默认使用 UTF-8。另请参阅 issue 46问题 53。 Facelets 目前的版本为 1.1.15.B1

至于您尝试修复它,XML 序言的存在并不是绝对必要的,并且其编码不会以任何方式用于设置响应编码,它仅由 XML 解析器用于将输入流解码为字符。 Spring的过滤器也不是必需的,但是添加它后并没有解决问题,这足以证明是客户端以ISO-8859-1发送了数据。

The symptoms indicate that the browser has sent the data using ISO-8859-1 encoding instead of UTF-8. This in turn means that the HTTP response Content-Type header is not been set with the proper charset attribute. In for example Firebug, you can find it out as follows:

enter image description here

You're right that Facelets uses UTF-8 by default. But very early versions of Facelets weren't programmed to use UTF-8 by default. See also among others issue 46 and issue 53. Facelets is currently at 1.1.15.B1.

As to your attempts to fix it, the presence of the XML prolog is not strictly necessary and its encoding isn't used in any way to set the response encoding, it's only used by the XML parser to decode the inputstream to characters. Spring's filter is also not necessary, but that it didn't solve the problem after you added it is enough evidence that it's the client who has sent the data as ISO-8859-1.

风尘浪孓 2024-12-29 16:32:40

检查您的表单是否有 enctype="multipart/form-data"

请参阅此问题 填写更多信息

Check, if your form has enctype="multipart/form-data".

See this question form more information

红玫瑰 2024-12-29 16:32:40
import org.springframework.web.filter.CharacterEncodingFilter;
@Bean
    FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setUrlPatterns(Collections.singletonList("/*"));
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registrationBean.setFilter(filter);
        return registrationBean;
    }

就像 Elias Dorneles 建议将其作为链中的第一个过滤器一样。以下是使用代码配置CharacterEncodingFilter 时的操作方法。

import org.springframework.web.filter.CharacterEncodingFilter;
@Bean
    FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setUrlPatterns(Collections.singletonList("/*"));
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registrationBean.setFilter(filter);
        return registrationBean;
    }

Like Elias Dorneles suggested to put it as the first Filter in the Chain. Here is how you do it when configuring the CharacterEncodingFilter with code.

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