• cdi
  • components
  • contexts
  • converters
  • el
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • push
  • resourcehandlers
  • scripts
  • servlets
  • taghandlers
  • utils
  • validators
  • viewhandlers
 - 
  • CacheControlFilter
  • CharacterEncodingFilter
  • FacesExceptionFilter
  • GzipResponseFilter
  • HttpFilter

Available since OmniFaces 1.2

This filter will set the request body character encoding when not already set by the client. Even though JSF2/Facelets uses by default UTF-8 everywhere, which is the best charset choice these days, JSF2/Facelets might fail to set it to UTF-8 when something else has set it to a different value before JSF2/Facelets gets the chance to set it during the restore view phase. PrimeFaces 3.x for example is known to do that. During ajax requests, it will call ExternalContext.getRequestParameterMap() inside PartialViewContext.isAjaxRequest() right before building/restoring the view, which will implicitly set the request body character encoding to the server-default value, which is not UTF-8 per se.

Installation

To get this filter to run, map it as follows in web.xml:

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.omnifaces.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Configuration (optional)

As JSF2/Facelets uses by default UTF-8 everywhere, the default charset is also set to UTF-8. When really necessary for some reason, then it can be overridden by specifying the encoding initialization parameter in the <filter> element as follows:

<init-param>
    <description>The character encoding which is to be used to parse the HTTP request body. Defaults to UTF-8.</description>
    <param-name>encoding</param-name>
    <param-value>ISO-8859-1</param-value>
</init-param>

Please note that this only affects HTTP POST requests, not HTTP GET requests. For HTTP GET requests, you should be specifying the charset at servletcontainer level (e.g. <Context URIEncoding="UTF-8"> in Tomcat, or <parameter-encoding default-charset="UTF-8"> in Glassfish, or <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true" /> in JBoss in combination with this filter). Also note that this doesn't affect HTTP responses in any way. For HTTP responses, you should be specifying the charset in <f:view encoding>, which also already defaults to UTF-8 by the way.

See also:
PrimeFaces issue 2223
Unicode input retrieved via PrimeFaces input components become corrupted

Demo

The CharacterEncodingFilter is also configured on this showcase application. You can see it by special characters being properly dealt in POST forms throughout the showcase application and by the presence of the filter in stacktraces of showcase pages demonstrating some exceptions.