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.

[3557 byte] By [snailxua] at [2007-10-2 21:10:28]
# 1

I just realize that this may not be the best solution.

In my entire web application, I have uncountable JSF components containing german umlaute, if this solution is used, then I need to insert uncountable <f:converter converterId="myConverter"></f:converter> to everyplace where umlaute will occur. It is tedious and time cost job.

does any one has better idea, like, I only need to do once, then it works anywhere.

snailxua at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Write converter for type java.lang.String and it`ll be invoked automaticaly without writing f:converter everytime
amitteva at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

thanks amittev.

but as far as I know, to use a custom converter, the page author must refer to the Converter from the tag of the component whose data must be converted.

could u provide me more info how to invoke MyConverter (it is actually the converter for type java.lang.String, is not it?) automatically without registering f:converter in component?

snailxua at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

It's possible to register a converter for a class rather than with an id

In your faces-config.xml:

<converter>

<converter-for-class>java.lang.String</converter-for-class>

<converter-class>example.YourConverter</converter-class>

</converter>

brynjarglesa at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

brynjargles, thank you for ur hint.

I have tried to register converter for java.lang.String, it does not work even for the simplest example.

On my page, there are one input text box, one submit button, one output text box to display the inputed value, here is the part of page source code:

<h:inputText styleClass="inputText" id="text2" value="#{requestScope.input}"></h:inputText>

<hx:commandExButton type="submit" value="Submit"

styleClass="commandExButton" id="button1"></hx:commandExButton>

<h:outputText id="text1" value="#{requestScope.input}" styleClass="outputText" >

</h:outputText>

In the faces-config.xml,

<converter>

<converter-for-class>java.lang.String</converter-for-class>

<converter-class>converter.MyConverter</converter-class>

</converter>

After the page is loaded, I type "M黱chen" into the input box, click the submit, M眉nchen is displayed without invoking MyConverter.

I am wondering, it may not be possible to register a converter for java.lang.String.

Does someone have any idea?

snailxua at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I have the same problem with you but I found that only sandbox components have the encoding problems. Your solution can't solve my problem. I have googled for about 10 days and I couldn't find any solutions for this.
zhlmmca at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
One comment:In my case, this problem occurs only in the test environmnet. After I deploy my application into Productive environment, the encoding is automactically handled by the server.
snailxua at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Hello,When i give an input from JSP to a Servlet with german characters, the result is fine. But when i give input from a batch file, the German characters becomes junk. Can any one please give me a solution?RegardsVenkat
venkat_ragha at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

I had the same problem with german umlauts.

The content of an h:inputTextarea has been encoded wrong when I wanted to work with the String value.

Then I set up this at the top of the JSF Page:

<%@ page contentType="text/html;charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

After that it worked without Problems.

aellabaetscha at 2007-7-13 23:56:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...