【发布时间】:2014-01-21 01:04:43
【问题描述】:
我刚刚开始学习 JSF 和 PrimeFaces,一旦我解决了一个问题(在您的帮助下),就会出现另一个问题。我有一个数据表,其中显示了有关我的应用程序用户的一些数据;在最后一列中,commandButton 调用允许编辑相应数据的对话框。该对话框实际上与支持 bean 交互,因为字段已使用现有数据正确预编译,但“提交更改”命令按钮不会触发正确的 editUser() 方法!
我到处寻找解决问题的方法,但 PrimeFaces 论坛上的帖子和 Stack Overflow 上的任何问题都没有帮助我:我尝试了action、actionListener、内部@987654324 的所有组合@,外层<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
}
【问题讨论】:
-
如果你能发布更多你的 xhtml 就好了
-
添加了更多的 xhtml 和一些额外的注释!再次感谢你。 @LeonardoKenji
-
对话框必须有自己的形式。
-
其实,@BalusC,我想通了!我已经尝试过将对话框放在单独的表单中,以及将对话框置于表单之外并将一个 inside 放在对话框中,但没有成功。我已经在我的回答中列出了对我有用的安排。还是非常感谢! :)
标签: jsf-2 primefaces