Daniel
Daniel

Reputation: 854

Wrong Character Set for JSF's h:inputText on first submit (only)

In the following form, we try to return a user's input to JSF's h:inputText or PrimeFaces' p:inputText. We experience strange behavior when non-Latin characters (Japanese, Hebrew, etc. ) are entered:

On first request we get unrecognized character set, but on the second request - we get a correct result.

Input/Output Examples (first run only):

  1. Japanese: input = 日 output = æ¥

  2. Hebrew: input = א output = ×

JSF:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <body>
        <h:form>   
            <h:outputLabel value="Name:"/>                        
            <h:inputText value="#{newTestController.registeredCustomerFirstName}"/>
            <h:commandButton value="Continue" action="#{newTestController.RegisteredNewCustomer(actionEvent)}"/>
        </h:form> 
    </body>
</html>

Backing Bean:

@ManagedBean(name = "newTestController")
@SessionScoped
public class NewTestController {

    private String registeredCustomerFirstName;

    public String getRegisteredCustomerFirstName() {
        return registeredCustomerFirstName;
    }

    public void setRegisteredCustomerFirstName(String registeredCustomerFirstName) {
        this.registeredCustomerFirstName = registeredCustomerFirstName;
    }

    public void RegisteredNewCustomer(ActionEvent actionEvent) throws Exception {
    }
}

Upvotes: 2

Views: 6132

Answers (3)

user1801069
user1801069

Reputation: 75

Specifying charset in the config file might be not enough. Try using p:commandButton instead of h:commandButton. The p:commandButton by default uses ajax, while the h:commandButton does non-ajax submit.

Upvotes: 0

edburns
edburns

Reputation: 532

This is related to < http://java.net/jira/browse/GLASSFISH-18007 >. That fix was made to prevent a warning message when we unconditionally set the encoding to UTF-8, which would seem to be what we want, but in this case we felt it safer to not do it.

I've created a related issue in Mojarra, < http://java.net/jira/browse/JAVASERVERFACES-2217 >. Bottom line: setting the encoding explicitly in the app configuration is the right solution. The implementation is already doing the right thing.

Upvotes: 0

Daniel
Daniel

Reputation: 854

As commented above - it is needed to define a default-charset for the application server.

For glassfish: add <parameter-encoding default-charset="UTF-8" /> to glassfish-web.xml.

For other application servers see BalusC's blog regarding this issue.

Upvotes: 4

Related Questions