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

查看:97
本文介绍了在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的编辑器组件键入中文

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 Typing Chinese with PrimeFaces' editor component.

其中一个解决方案是创建一个 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天全站免登陆