1

I'm using PrimeFaces 3.4 with MyFaces 2.0.7 on WebSphere 7. I'm using filter to set the response encoding to UTF-8:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
<filter-mapping>
    <filter-name>CharsetEncodingFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

In logs I see the filter is called on each JSF resource set, including resoures such as:

http://localhost:9080/myapp/javax.faces.resource/myscript.js.xhtml?ln=mylib

However, when I see the response headers, charset is not set, only the Contet-Type is set to application/x-javascript. My filter is declared as first in web.xml, the only other I'm using is org.primefaces.webapp.filter.FileUploadFilter which does nothing with character encoding.

The JavaScript files are treated as UTF-8 as default because messages are displayed correctly, but nevertheless I would like the character set to be set correctly.

How should I do that? The filter was always working for me, why not in JSF/PrimeFaces? Am I missing something with this approach?

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
  • No, I don't expect content type to be set because I know in this filter nothing about the possible content type. I can set only the encoding. – Danubian Sailor Feb 14 '13 at 12:48

1 Answers1

0

I think that there is some mess with your <filter>/<servlet> declaration

Servlet sould be

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

and Filter should be

<filter>
    <filter-name>Your filter</filter-name>
    <filter-class>com.yourpackage.YourFilterClass</filter-class>
</filter>
<filter-mapping>
    <filter-name>Your filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

Also, as you are using JSF the headers are set up in your views, most probably *.xhtml files. You can explicitly set character encoding there:

<h:head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF8" />
</h:head>

Edit

It was my misunderstanding of your question.

It seems that there is a bug in Primefaces stemming from use of <h:form enctype="multipart/form-data">. MultipartRequest object uses platform's default encoding instead of encoding set on request object by a filter.

See this answer for analysis and a workaround. Another workaround is setting UTF-8 as default encoding of your server.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Please read my question, the filter is working and is calling setCharacterEncoding, and I'm dealing with JavaScript resources, not jsf pages. – Danubian Sailor Feb 14 '13 at 08:50
  • Pardon, my fault. I prefer using `` in ``, so the initial wrong comment was born. It turns out that `setCharacterEncoding` is called in a filter, but is later ignored. – skuntsel Feb 14 '13 at 09:47
  • Yes, as if it was overwritten. For JSF pages the meta works, but for resources via outputScript the filter seems the only way, that is not functioning... – Danubian Sailor Feb 14 '13 at 10:07
  • Unfortunately, yes. Basically, there are three ways of coping with the situation. 1. Set server encoding to UTF-8. 2. Follow the hacks of Primefaces, as suggested by BalusC. 3. Somehow use Primefaces' file upload component separately. – skuntsel Feb 14 '13 at 10:26