Problem of German Umlaute in JSF and its solution
Hello 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: 謘terreich and K鰈n (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鰈n" itemLabel="K鰈n" />
<f:selectItem itemValue="謘terreich" itemLabel="謘terreich"/>
</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鰈n is encoded into k胠n.
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胠n 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 揷onverter?
2. Change the getAsObject and getAsString methods as follows:
public Object getAsObject (FacesContext context, UIComponentcomponent, 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鰈n" itemLabel="k鰈n" />
<f:selectItem itemValue="謘terreich" itemLabel="謘terreich" />
</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 SystemOutO inside the converter...........
[28.05.06 16:48:37:828 CEST] 47b64c6b SystemOutO ..... before value: K枚ln
[28.05.06 16:48:37:828 CEST] 47b64c6b SystemOutO ..... after value: K鰈n
Cheers!!!
If someone has better solution, I'd like to share ur idea.

