Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 form submit in JSF is corrupting data [duplicate]

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?

like image 270
Shyam Avatar asked May 23 '12 13:05

Shyam


1 Answers

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

    // ...
}
like image 93
BalusC Avatar answered Oct 29 '22 02:10

BalusC