我刚刚开始学习 JSF 和 PrimeFaces,一旦我解决了一个问题(在您的帮助下),另一个问题就会出现。我有一个数据表显示有关我的应用程序用户的一些数据;在最后一列中,commandButton 调用允许编辑相应数据的对话框。该对话框实际上与支持 bean 交互,因为字段已使用现有数据正确预编译,但“提交更改”命令按钮不会触发正确的 editUser()
方法!
我到处搜索我的问题的解决方案,但 PrimeFaces 论坛上的帖子和 Stack Overflow 上的任何问题都没有帮助我:我尝试了 action
的所有组合, actionListener
, 内部 <h:form>
, 外层 <h:form>
,甚至是可怕的嵌套 <h:form>
,但仍未调用底层方法。
谢谢大家!
编辑:我包含了更多的 xhtml。需要明确的是:在数据表中,我同时实现了单选和多选机制。单个选择由 editButton
执行在最后一列并触发 editDialog
这让我很痛苦,而第一列中的复选框启用了多项选择,并且表格底部的命令按钮将其作为目标,该命令按钮删除了所有选定的用户;当然,它们将选择存储在支持 bean 的不同字段中(分别为 selectedUser
和 selectedUsers[]
)。
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
}
最佳答案
实际上,我唯一没有想到的就是那个有效的方法。 我通过使用三种形式解决了我的问题(正如我在问题中提到的,我已经尝试了一种和两种形式的所有可能组合,甚至是嵌套形式),如下所示:
<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>
看起来每个人都以对其他人不起作用的方式解决这个问题:)。
关于jsf-2 - 对话框内的 Primefaces commandButton 不触发支持 bean 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21247272/