Tuesday, November 13, 2007

Dynamic Faces and encoding problems

Dynamic Faces is part of the JSF extensions project, and it offers an easy way to ajaxify plain old JSF components. However, there is a problem (I would call it a bug) with international characters. Even if you specify UTF-8 as the encoding of the HTML/JSP page, the partial AJAX response is always in ISO-8859-1. This can be easily verified with the Netbeans HTTP monitor: under the Session tab, the value for the javax.faces.request.charset is UTF-8 before the request, and changes to ISO-8859-1, after the request. This results in lots of questionmarks or strange symbols in the page and it actually makes it impossible to use Dynamic Faces with an internationalized application.

The solution came from Matthias Unverzagt's comment in a number of blogs/forums, such as Hong's blog. Matthias suggests that the line

context.getExternalContext().setResponseCharacterEncoding("UTF-8");

needs to be added to the com.sun.faces.extensions.avatar.lifecycle.AsyncResponse's installOnOffResponse method.

This proved quite of a challenge for me, since it required recompilation of the jsf-extensions-dynamic-faces-0.1.jar, which was a bit tricky, as this jar appeared in a number of locations on my system:

  • ~/.netbeans/6.0beta2/config/org-netbeans-modules-visualweb-complib/installed-complibs/dynamicfaces02
  • ${netbeans_installation_dir}/visualweb1/modules/ext
The jars in these directories were different. The first directory also contained a jar with the sources, but NetBeans was using the jar from the second directory for my project.

So, here are the steps I followed (with NetBeans Beta2):

  1. I extracted jsf-extensions-dynamic-faces-0.1-sources.jar, found in the first directory above.
  2. I created a new Netbeans Java project with the extracted sources.
  3. In order to compile this project a number of libraries and jars had to be added to the project's classpath:
    • libraries: JSF 1.2, Web UI Components
    • jars el-api.jar, jsp-api.jar, servlet-api.jar, shale-remoting.jar
    The first 3 jars can be found in tomcat's lib directory, while the last one is located in ~/.netbeans/6.0beta2/config/org-netbeans-modules-visualweb-complib/installed-complibs/dynamicfaces02
  4. I opened the file AsyncResponse.java file, from the com.sun.faces.extensions.avatar.lifecycle package and added the line
    context.getExternalContext().setResponseCharacterEncoding("UTF-8");
    to the installOnOfResponse method's body. It probably doesn't matter where exactly you place this line; I added it as the second line of this method, right between the other two statements of this method.
  5. I extracted the jsf-extensions-dynamic-faces-0.1.jar file from the ${netbeans_installation_dir}/visualweb1/modules/ext directory and replaced the .class files in the lifecycle subdirectory with the ones I just compiled.
  6. That's all! I then recompiled and deployed my web application (so that the patched copy of jsf-extensions-dynamic-faces-0.1.jar would be used) and I was set.

4 comments:

Amit Kumar Saha said...

hi !

I am Amit Kumar Saha, NetBeans Community Docs contribution coordinator. Your blog post would make a fantastic entry in the "Tips & tricks" section of NetBeans Community Docs (http://wiki.netbeans.org/wiki/view/CommunityDocs)

Would you be willing to contribute it?

In case of any queries, please mail me here at amitsaha.in@gmail.com

Thanks,
Amit

Anonymous said...

Thx so much for this info.
I was frustrated, as I could not convert to UTF even with using servlet filters.

Anyway, thats a hot fix. I expect an international-friendly release of dyna-faces soon enough

Anonymous said...

thanks for your post,
But actually i can't decompile the dynamic faces jar file i used jad to decompile the .classes files but it didn't work well.
So please i need the source files of the dynamic faces to add this line and recompile it as you described in the post or the new compiled jar if possible.
Please i need your help.

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!