在下面的表单中,我们尝试将用户的输入返回给JSF的h:inputText
或PrimeFaces'p:inputText
。
当输入非拉丁字符(日语,希伯来语等)时,我们会遇到奇怪的行为:
在第一次请求时,我们得到无法识别的字符集,但在第二次请求时 - 我们得到了正确的结果。
输入/输出示例(仅限首次运行):
日本: 输入=日 输出=æ¥
希伯来语: 输入=א 输出=×
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 {
}
}
答案 0 :(得分:4)
如上所述 - 需要为应用程序服务器定义default-charset。
对于glassfish:将<parameter-encoding default-charset="UTF-8" />
添加到glassfish-web.xml
。
对于其他应用程序服务器,请参阅BalusC关于此问题的blog。
答案 1 :(得分:0)
这与&lt; http://java.net/jira/browse/GLASSFISH-18007&gt;。当我们无条件地将编码设置为UTF-8时,这个修复是为了防止出现警告消息,这似乎是我们想要的,但在这种情况下,我们认为不这样做会更安全。
我在Mojarra创建了一个相关问题,&lt; http://java.net/jira/browse/JAVASERVERFACES-2217&gt;。底线:在app配置中明确设置编码是正确的解决方案。实施已经做了正确的事情。
答案 2 :(得分:0)
在配置文件中指定charset可能还不够。 尝试使用p:commandButton而不是h:commandButton。 p:commandButton默认使用ajax,而h:commandButton执行非ajax提交。