Problem of German Umlaute in JSF and its solution
843842May 28 2006 — edited Jan 16 2008Hello all,
I got the validator problem when I process german Umlaute (�,�,�) in JSF. I have been googling for almost one week, tried every possible solution found in jsf forums (such as change page charset, pageEncoding, create filter to set the request characterEncoding to "UTF-8"), but still got no luck. Today, I finally solve this problem after many tries. I would like to share my experience, and hope u will not waste a lot of time on this issue again. This is also suitable for other languages, like chinese, for example.
Problem Scenario:
On the test.jsp page, I have a SelectOneListBox with two select items: �sterreich and K�ln (the itemLabel and itemValue are the same). The value of SelectOneListBox is assigned to the requestScope variable "input", the source code is:
<h:selectOneListbox styleClass="selectOneListbox" id="listbox1" value="#{requestScope.input}">
<f:selectItem itemValue="K�ln" itemLabel="K�ln" />
<f:selectItem itemValue="�sterreich" itemLabel="�sterreich"/>
</h:selectOneListbox>
Besides this, I draw and drop the command buttom, and the RequestScope input variable onto the page to display what I have slected in the Listbox after press the command buttom.
Everytime, when I select one of them, I got the following error message:
Validation Error: Value is not valid. From the cosole, I saw K�ln is encoded into k�ln.
Solution:
According to the JSF life cycle, I know the problem occurs on the Process Validations phase. So, what we can do is to change the k�ln into the correct one before it is past to validator phase.
Usually, the Converter happens before Validator, so
1. I created "MyConverter" which implements javax.faces.converter.Converter interface under the new package �converter�;
2. Change the getAsObject and getAsString methods as follows:
public Object getAsObject (FacesContext context, UIComponent component, String value) {
System.out.println("inside the converter...........");
System.out.println("..... before value: " + value);
try{
value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
}catch(Exception e){}
System.out.println("..... after value: " + value);
return value;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
return value.toString();
}
3. Register the MyConverter into the faces-config.xml file, like:
<converter>
<converter-id>myConverter</converter-id>
<converter-class>converter.MyConverter</converter-class>
</converter>
4. On the test.jsp page, insert the converter tag into jsp page source,
<h:selectOneListbox styleClass="selectOneListbox" id="listbox1" value="#{requestScope.input}">
<f:converter converterId="myConverter"></f:converter>
<f:selectItem itemValue="k�ln" itemLabel="k�ln" />
<f:selectItem itemValue="�sterreich" itemLabel="�sterreich" />
</h:selectOneListbox>
5. When u run this JSF page, u will get the correct value, on the console, I see that:
[28.05.06 16:48:37:828 CEST] 47b64c6b SystemOut O inside the converter...........
[28.05.06 16:48:37:828 CEST] 47b64c6b SystemOut O ..... before value: K��ln
[28.05.06 16:48:37:828 CEST] 47b64c6b SystemOut O ..... after value: K�ln
Cheers!!!
If someone has better solution, I'd like to share ur idea.