2011-03-29 131 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