2011-03-29 21 views
2

我有一個簡單的表單,帶有一個inputText和2個commandButtons。 inputText顯示支持bean的值正確,但是當我第一次改變該值時,set方法不被調用,因此表單提交一個空值。當我再次更改它時,則調用set方法,並且一切正常。什麼原因,我該如何解決?<h:inputText值不更新

<h:panelGroup id="chatId"> 
    <h:panelGrid rendered="#{chat.validUser == true}"> 
     <h:form id="sendMsgForm" > 
     <h:panelGroup id="chatId2"> 
      <h:inputText id="chatInput" value="#{chat.msgTo}" autocomplete="off" > 
      <f:ajax execute="@this @form" /> 
      </h:inputText> 
      <h:commandButton value="Send" id="sendButton" action="#{chat.send}"> 
      <f:ajax render=":chatLogId :chatId" /> 
      </h:commandButton>  
      <h:commandButton value="Bye" action="#{chat.bye}"> 
      <f:ajax render=":chatLogId :chatId :chatUserId" /> 
      </h:commandButton> 
     </h:panelGroup> 
     </h:form> 
    </h:panelGrid> 
    </h:panelGroup> 

的支持bean代碼:

@SessionScoped 
public class Chat implements Serializable, ActionListener, ValueChangeListener { 
    private String msgTo = "Start typing..."; 
    private boolean validUser = false; 

    public void bye() { 
    validUser = false; 
    tc.disconnect(); 
    } 

    public void send() { 
    try { 
     tc.sendMessage(msgTo); 
     setMsgTo(""); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    // ... 

回答

0

也許是因爲你火的同時併發行爲的兩個Ajax調用。如果你點擊sendButton,chatId面板組將被重新渲染(通過你提交按鈕的ajax調用)並且執行表單(通過來自輸入域的ajax調用)。

嘗試爲您的提交按鈕的Ajax調用以下內容:由Matt手持

<h:commandButton value="Send" id="sendButton" action="#{chat.send}"> 
    <f:ajax render=":chatLogId :chatId" execute="@form"/> 
</h:commandButton> 
3

的代碼片段是正確的解決方案,但病因的解釋是不正確。

您在命令按鈕中忽略了<f:ajax>execute屬性。它將默認爲@this,這意味着只有按鈕的名稱=值對本身被髮送到服務器端(因此只有相關的動作將被調用;輸入值將不會被更新)。由於您想要提交整個表單,因此您需要明確將execute設置爲@form

<h:commandButton value="Send" id="sendButton" action="#{chat.send}"> 
    <f:ajax execute="@form" render=":chatLogId :chatId" /> 
</h:commandButton> 

它在輸入字段更改期間工作是因爲您已將execute="@form"代替輸入字段。輸入字段內的<f:ajax>將在您更改該值時默認執行。但在這種特殊情況下,你完全不需要它。所以擺脫它:

<h:inputText id="chatInput" value="#{chat.msgTo}" autocomplete="off" /> 
+0

但他在第一次和第二次更改輸入字段值。所以它應該在兩個時間都有效。或者我錯過了什麼? – 2011-03-30 06:32:09

+0

感謝您對Matt和BalusC的回覆。我試圖在commandButton中只有f:ajax,並且只是inputText,但行爲仍然相同。當我第一次改變輸入字段並按下回車鍵,或者點擊按鈕時,輸入字段被重置爲默認的「開始輸入...」。第二次,我改變了輸入字段,一切正常。這就像我的輸入是第一次在別的地方。謝謝。 Binh – Binh 2011-03-30 10:14:39

+0

這些更改是否重新發布到服務器? – BalusC 2011-03-30 11:11:16

0

想跟進我的問題的答案是鞏固我有兩種形式的3個形式標籤。 chatLogId有1個表單,chatId有1個表示,chatUserId有1個表單。 chatLogId只是一個數據表,我只剩下它。我將chatId和chatUserId表單合併爲一個。現在,當我離開inputText字段時,將調用setMsgTo,然後調用send方法。不確定,但我認爲我的問題可能是之前更新了錯誤的表單。 感謝您的幫助。 Binh