【问题标题】:Primefaces commandButton inside dialog not firing backing bean's method对话框内的 Primefaces commandButton 未触发支持 bean 的方法
【发布时间】:2014-01-21 01:04:43
【问题描述】:

我刚刚开始学习 JSF 和 PrimeFaces,一旦我解决了一个问题(在您的帮助下),就会出现另一个问题。我有一个数据表,其中显示了有关我的应用程序用户的一些数据;在最后一列中,commandButton 调用允许编辑相应数据的对话框。该对话框实际上与支持 bean 交互,因为字段已使用现有数据正确预编译,但“提交更改”命令按钮不会触发正确的 editUser() 方法!

我到处寻找解决问题的方法,但 PrimeFaces 论坛上的帖子和 Stack Overflow 上的任何问题都没有帮助我:我尝试了actionactionListener、内部@987654324 的所有组合@,外层<h:form>,甚至是可怕的嵌套<h:form>,但是底层的方法仍然没有被调用。

谢谢大家!

编辑:我添加了更多的 xhtml。需要明确的是:在数据表中,我同时实现了单选和多选机制。单选由最后一列中的editButton 执行并触发editDialog,这让我很痛苦,而多选由第一列中的复选框启用,并由表格底部的命令按钮定位删除所有选定的用户;当然,它们将选择存储在支持 bean 的不同字段中(分别为 selectedUserselectedUsers[])。

xhtml 文件

<h:form id="tableForm">
    <p:dataTable id="userList" var="user" value="#{userListBean.userList}"
        selection="#{userListBean.selectedUsers}" rowKey="#{user.username}">

        <!-- this is a checkbox column I use for multiple selection -->
        <p:column selectionMode="multiple" style="width:2%"/>

        <!-- other datatable columns -->

        <!-- this is the button column that triggers the dialog -->
        <p:column style="width:4%">
            <p:commandButton id="editButton" update=":tableForm:editUserData"
                oncomplete="PF('editDialog').show()" title="Edit" icon="ui-icon-pencil">
                <f:setPropertyActionListener target="#{userListBean.selectedUser}"
                    value="#{user}" />
            </p:commandButton>
        </p:column>
    </p:datatable>

    <p:dialog id="editDlg" widgetVar="editDialog" header="Edit User"
        showEffect="fade" hideEffect="fade" modal="true" dynamic="true">
        <h:panelGrid columns="6" id="editUserData">

            <p:outputLabel for="editUsername">Username:</p:outputLabel>
            <p:inputText disabled="true" id="editUsername" value="#{userListBean.selectedUser.username}" />
            <p:message for="editUsername" />

            <!-- and other fields like that -->

        </h:panelGrid>

        <p:commandButton id="submitChanges" action="#{userListBean.editUser()}"
            value="Submit changes" oncomplete="PF('editDialog').hide();" /> 
    </p:dialog>
</h:form>

支持 bean

@ManagedBean(name="userListBean")
@ViewScoped
public class UserListBean {
    private UserDTO selectedUser;

    public UserListBean() {

    }

    //some methods...

    public String editUser() {
        System.out.println("------------------ EDIT TRIGGERED! -------------------");
        System.out.println(selectedUser.getUsername());
        //this stuff never gets printed, so the method is never called!
    }

    //getters and setters
}

【问题讨论】:

  • 如果你能发布更多你的 xhtml 就好了
  • 添加了更多的 xhtml 和一些额外的注释!再次感谢你。 @LeonardoKenji
  • 对话框必须有自己的形式。
  • 其实,@BalusC,我想通了!我已经尝试过将对话框放在单独的表单中,以及将对话框置于表单之外并将一个 inside 放在对话框中,但没有成功。我已经在我的回答中列出了对我有用的安排。还是非常感谢! :)

标签: jsf-2 primefaces


【解决方案1】:

实际上,我唯一没有想到的是那个有效的。 我使用三种形式解决了我的问题(正如我在问题中提到的,我已经尝试了一种和两种形式的所有可能组合,甚至是嵌套的),如下所示:

<h:form id="tableForm">
    <!-- here lies the p:dataTable -->
</h:form>

<p:dialog>
    <h:form id="dialogForm">
        <!-- here lies the h:panelGrid with the editable fields -->
    </h:form>
    <h:form id="buttonForm">
        <!-- and HERE goes the commandButton, alone -->
    </h:form>
</p:dialog>

看起来每个人都以不适合其他人的方式解决这个问题:)。

【讨论】:

  • 这不会提交来自可编辑字段的数据。这反过来又对您“有效”表明您从未显示过面孔消息的可编辑字段之一中存在验证/转换错误。
  • 将命令按钮直接放在表单标签下也对我有用。干杯。
猜你喜欢
  • 2015-06-13
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 2016-10-27
  • 2011-08-15
  • 2015-02-11
  • 1970-01-01
相关资源
最近更新 更多