在 JSF 中提交的 UTF-8 表单会破坏数据 [英] UTF-8 form submit in JSF is corrupting data

查看:17
本文介绍了在 JSF 中提交的 UTF-8 表单会破坏数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其中一个项目中,我在表单数据上提供了非英语内容(芬兰语).我们将 JSF 2.0 与 PrimeFaces 一起使用.向服务器提交数据时遇到问题.提交表单时数据已损坏.只有芬兰字符在这方面变得腐败.

In one of the projects I have non-English content (Finnish) available on form data. We are using JSF 2.0 with PrimeFaces. I have trouble when submitting the data to the server. The data is getting corrupted when I submit the form. Only the Finnish characters are getting corrupt in that.

有没有人已经遇到过这个问题并找到了解决方案?

Has anyone faced this issue already and found a solution?

推荐答案

这是自 PrimeFaces 3.0 以来的已知问题.这是由检查当前 HTTP 请求是否为 ajax 请求的方式发生变化引起的.它由请求参数而不是请求标头标识.在恢复 JSF 视图之前第一次检索请求参数时,将使用服务器的默认字符编码(通常是 ISO-8859-1,而不是 JSF 自己的默认字符编码 UTF-8)解析所有请求参数.如需深入解释,请参阅通过 PrimeFaces 输入组件检索的 Unicode 输入已损坏.

This is a known problem since PrimeFaces 3.0. It's caused by a change in how it checks if the current HTTP request is an ajax request. It's been identified by a request parameter instead of a request header. When a request parameter is retrieved for the first time before the JSF view is restored, then all request parameters will be parsed using server's default character encoding which is often ISO-8859-1 instead of JSF's own default character encoding UTF-8. For an in depth explanation see Unicode input retrieved via PrimeFaces input components become corrupted.

解决方案之一是创建一个过滤器,它执行 request.setCharacterEncoding("UTF-8").

One of the solutions is to create a filter which does a request.setCharacterEncoding("UTF-8").

@WebFilter("*.xhtml")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    // ...
}

这篇关于在 JSF 中提交的 UTF-8 表单会破坏数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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