UTF-8 form submit in JSF is corrupting data [dupli

2019-01-14 23:18发布

This question already has an answer here:

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?

1条回答
可以哭但决不认输i
2楼-- · 2019-01-15 00:19

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.

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);
    }

    // ...
}
查看更多
登录 后发表回答