【问题标题】:Bizarre bug: inputTextarea only hook its value to the last row in dataTable奇怪的错误:inputTextarea 仅将其值挂钩到 dataTable 中的最后一行
【发布时间】:2010-10-15 20:18:00
【问题描述】:

错误是我在下面的inputTextarea。只有最后一行可以记录来自inputTextarea 的任何输入,也就是说,只有最后一行,将value 挂钩到#{PostComment.comment.comment}。这是为什么呢?

<h:form id="userCommentList" >
   <p:dataTable value="#{CentralFeed.profileComments}" var="item">
        <p:column>        
           <!-- Original Comment -->         
           <h:outputText value="#{item.comment}"/> &nbsp;              

           <!-- ****BUG IS THIS INPUTTEXTAREA BELOW*** -->
           <h:inputTextarea id="keyword" value="#{PostComment.comment.comment}"/> &nbsp;

           <p:commandButton actionListener="#{PostComment.postProfileComment(item.id)}"
                                             value="Post" update="userCommentList" />
        </p:column>                                                                                   
   </p:dataTable>
</h:form>

编辑 我改变了inputTextareacommandButton 就像你建议的BalusC。在Comment 实体中,我添加了另一个字段调用newComment,所以Comment 看起来像这样

Comment
+ id
+ comment
+ newComment --> I have @Transient to this field so it wont map to the database. I also set its default value to the empty string in the constructor
+ ...

...
<p:column>
    ...
    <h:inputTextarea id="keyword" value="#{item.newComment}" rows="2" />
    <p:commandButton actionListener="#{PostComment.postReply(item)}" value="Post" />
</p:column>

我希望item.newComment 能保存我刚刚输入的值,所以当我将对象item 传递给postReply 时,我可以将newComment 的内容提取出来,但是它是一个空字符串。所以无论我输入什么都不会绑定到newComment。知道为什么吗?

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

    您实质上是将表格中多个文本区域的值绑定到一个相同 bean 属性。在请求处理期间,JSF 将使用表中 all textareas 的值更新此属性,每次都覆盖前一个,直到最后一行中的一个。这就是为什么您只会在最后一行看到一个值。

    您需要将 textarea 值绑定到行对象,在您的情况下为 #{item}。例如

    <h:inputTextarea id="keyword" value="#{item.newComment}"/>
    

    【讨论】:

    • newComment 是什么意思,还是只是我的对象 BalusC 中的一个字段?
    • 这只是一个例子。你甚至可以使用#{item.comment.message}。只要它以String 属性结尾。
    • 提交的值是否作为请求参数存在? (通过 Firebug、Fiddler 或 getRequestParameterMap() 检查)。我也许还应该注意,我仍然假设您没有更改代码的其他部分,因为只有最后一个值被提交的原始问题,例如添加嵌套表单、渲染/禁用属性等。
    • 好的,已经提交了,但是模型没有更新。输入或按钮上没有immediate="true" 吗?去掉它。如果这不是原因,我将开始调试以查看数据模型的 getter 是否返回了正确的数据模型,并且无论如何都调用了 newComment 的 setter 以及在正确的阶段。我怀疑数据模型加载/保存中存在错误,并且无法找到模型或者它已被新的重新加载覆盖。
    • 在 getter 中记录 FacesContext#getCurrentPhaseId() 或在调试器的变量部分中确定它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多