11

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69
  • Are you uploading a file during submit?I develop apps in Czech language and this happened to me with IceFaces file upload component. But in normal cases was everything fine. You could use Filter for setting Finnish charset but it's still strange error – Petr Mensik May 23 '12 at 13:52

1 Answers1

20

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

    // ...
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It worked for me with the following annotation : @WebFilter(filterName ="CharacterEncodingFilter", urlPatterns = {"/*"}) Thanks – Kiavash Oct 03 '12 at 06:38
  • 2
    @Kia: apparently your `FacesServlet` is not been mapped on `*.xhtml`, but on something else such as `/faces/*` or `*.jsf`. You should then change the URL pattern of the filter to be the same. The `/*` will indeed match **every single** HTTP request. By the way, just `@WebFilter("/*")` was also sufficient. – BalusC Oct 03 '12 at 10:15