users@javaserverfaces.java.net

Re: Scrambled umlauts in input text field

From: Alexander Hartner <alex_at_j2anywhere.com>
Date: Fri, 2 Nov 2012 01:36:25 +0800

Are you setting the encoding on the top of each page ?

On 02/11/2012, at 12:21 AM, Holger Stenzhorn <holger.stenzhorn_at_gmail.com> wrote:

> Dear Alexander and all,
>
> Thank you for your advice but it did not solve the issue and so I further experimented:
>
> - "FacesContext.getCurrentInstance().getExternalContext().getRequestCharacterEncoding()" returns "UTF-8" as expected (without any change to the server configuration).
>
> - So I imagined that the string "value" might be (wrongly) encoded as "ISO-8859-1" (and not "UTF-8"). So, based on this assumption, I "tweaked" the setter for "value" as follows:
>
> public void setValue(String value) throws Exception {
> this.value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
> }
>
> The result shows that my assumption might be right: When I now enter the umlauts "äöü" in the input text field and I submit then those umlauts are not scrambled but redisplayed correctly.
>
> Hence, since the string "arrives" at the bean/setter already in the wrong encoding, I would guess that the input component does not properly encode the string before handing it off.
>
> Regards,
> Holger
>
> Am 01.11.12 01:31, schrieb Alexander Hartner:
>> You need to configure your tomcat / JVM to use UTF-8
>>
>> JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8"
>>
>> as well as the header of the page. This should fix it.
>>
>> On 01/11/2012, at 7:34 AM, Holger Stenzhorn<holger.stenzhorn_at_gmail.com> wrote:
>>
>>> Hello all,
>>>
>>> Using Mojarra 2.1.14, I have created the following JSF page (see [1]) and managed bean (see [2]).
>>>
>>> When I open that page then the umlauts ("äöü") on the command button are displayed correctly.
>>> Now I enter those same umlauts in the input text field and click on the button (see "before.png").
>>> The page is redisplayed with the umlauts in the input text field scrambled ("äöü") (see "after.png").
>>>
>>> Am I forgetting to do a certain setting here or is this a (possibly known) bug or issue?
>>> Thanks in advance for your help!
>>>
>>> Regards,
>>> Holger
>>>
>>> [1]
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <html xmlns="http://www.w3.org/1999/xhtml"
>>> xmlns:h="http://java.sun.com/jsf/html">
>>> <h:body>
>>> <h:form>
>>> <h:inputText value="#{bean.value}"/>
>>> <h:commandButton action="/pages/temp" value="äöü"/>
>>> </h:form>
>>> </h:body>
>>> </html>
>>>
>>> [2]
>>> package some.example;
>>>
>>> import javax.faces.bean.ManagedBean;
>>> import javax.faces.bean.RequestScoped;
>>>
>>> @ManagedBean(name="bean")
>>> @RequestScoped
>>> public class Bean {
>>>
>>> private String value;
>>>
>>> public String getValue() {
>>> return value;
>>> }
>>>
>>> public void setValue(String value) {
>>> this.value = value;
>>> }
>>> }
>>> <before.png><after.png>
>