位于对话框内的Primefaces commandButton无法触发支持Bean方法。

3
我刚开始学习JSF和PrimeFaces,每当我解决一个问题(在您的帮助下),另一个问题就会出现。我有一个数据表格显示了一些关于应用程序用户的数据;在最后一列,一个命令按钮调用对话框允许相应的数据进行编辑。该对话框实际上与支持Bean交互,因为字段使用现有数据正确预编译,但是"提交更改"命令按钮不会触发适当的editUser()方法!
我已经搜索到处寻找解决方案,但PrimeFaces论坛上的线程和Stack Overflow上的任何问题都没有帮助我:我尝试了所有组合actionactionListener,内部<h:form>,外部<h:form>,甚至可怕的嵌套<h:form>,但基础方法仍然未被调用。
谢谢大家!
编辑:我包含了更多的xhtml。只是为了清楚起见:在数据表格中,我正在实现单选和多选机制。单选由最后一列中的editButton执行,并触发引发痛苦的editDialog,而多选则由第一列中的复选框启用,并由表格底部的一个commandButton定位,该按钮删除所有选定的用户;当然,它们将选择存储在支持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>

Backing 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代码,那就太好了。 - user1073494
添加了更多的xhtml和一些额外的注释!再次感谢。@LeonardoKenji - thequanticlad
对话框必须有自己的窗体。 - BalusC
实际上,@BalusC,我搞定了!我已经尝试将对话框放在它自己的独立窗体中,以及将对话框保持在窗体之外并将一个窗体放置在对话框内部,但都没有成功。我已经在我的答案中列出了适合我的排列方式。无论如何,非常感谢! :) - thequanticlad
1个回答

2

实际上,我没想到的唯一解决方案竟然是有效的。我通过使用三个表单(正如我在问题中提到的,我已经尝试了所有可能的一种和两种表单的组合,甚至嵌套的情况),来解决了我的问题,就像这样:

<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>

看起来每个人解决这个问题的方法都不适用于其他人 :)


1
这不会提交可编辑字段中的数据。这意味着您的操作“有效”,但也表明其中一个可编辑字段存在验证/转换错误,而您从未显示面向用户的消息。 - BalusC
把commandButton直接放在form标签下面对我也起作用。干杯。 - Fuzzy Analysis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接