第一次提交时JSF的h:inputText错误的字符集(仅限)

问题描述 投票:2回答:3

在下面的表单中,我们尝试将用户的输入返回给JSF的h:inputText或PrimeFaces的p:inputText。输入非拉丁字符(日语,希伯来语等)时,我们会遇到奇怪的行为:

在第一次请求时,我们得到无法识别的字符集,但在第二次请求时 - 我们得到了正确的结果。

输入/输出示例(仅限首次运行):

  1. 日语:输入=日输出=æ¥
  2. 希伯来语:输入=א输出=×

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>

支持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 {
    }
}
jsf
3个回答
4
投票

如上所述 - 需要为应用程序服务器定义default-charset。

对于glassfish:将<parameter-encoding default-charset="UTF-8" />添加到glassfish-web.xml

对于其他应用程序服务器,请参阅BalusC的blog有关此问题。


0
投票

这与<http://java.net/jira/browse/GLASSFISH-18007>有关。当我们无条件地将编码设置为UTF-8时,该修复是为了防止发出警告消息,这似乎是我们想要的,但在这种情况下,我们认为不这样做会更安全。

我在Mojarra创建了一个相关的问题,<http://java.net/jira/browse/JAVASERVERFACES-2217>。底线:在app配置中明确设置编码是正确的解决方案。实施已经做了正确的事情。


0
投票

在配置文件中指定charset可能还不够。尝试使用p:commandButton而不是h:commandButton。 p:commandButton默认使用ajax,而h:commandButton执行非ajax提交。

© www.soinside.com 2019 - 2024. All rights reserved.