2012-03-10 45 views
2

在下面的表格中,我們嘗試將用戶的輸入返回給JSF的h:inputText或PrimeFaces的p:inputText。 當輸入非拉丁字符(日文,希伯來文等)時,我們會遇到奇怪的行爲:第一次提交JSF的h: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 { 
    } 
} 
+0

'h:inputText'來自標準的JSF。你不是指'p:inputText'嗎?至於這個問題,你是如何以及在哪裏確認角色是mojibake?在打印日誌並閱讀時?或者在提交後檢查重新顯示的表單時? – BalusC 2012-03-10 16:47:30

+0

對不起。這在'p:inputText'和'h:input'上都會發生,所以它可能與JSF相關,而不是相關的。至於這個問題:在'getRegisteredCustomerFirstName()'上重新顯示,記錄和設置斷點時就可以看到它。 – Daniel 2012-03-11 05:05:22

+0

我有一個沒有解釋的解決方案。對於glassfish,請在glassfish-web.xml中添加。儘管如此,爲什麼它只是第一次失敗呢? – Daniel 2012-03-11 13:00:30

回答

4

正如上面評論 - 它是需要來定義默認-字符集的應用程序服務器。

對於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>。底線:明確地在應用程序配置中設置編碼是正確的解決方案。實施已經在做正確的事情。

+0

你好,我使用Tomcat,但我有完全相同的問題。 – 2013-05-14 13:05:54

+0

是否按照問題2217中的說明在應用程序中設置配置不能解決那裏的問題? – edburns 2013-05-17 02:26:34

+0

請參閱:http://stackoverflow.com/questions/16544262/jsf-utf-8-characters-not-seen-only-in-first-form-post-jsf-tomcat @edburns謝謝。 – 2013-05-21 05:46:20

0

在配置文件中指定charset可能不夠。 嘗試使用p:commandButton而不是h:commandButton。 p:commandButton默認使用ajax,而h:commandButton則使用非ajax提交。

相關問題