2013-01-11 113 views
8

我有一个PrimeFaces数据表,它显示一组用户,并在每行上删除一个commandLink。点击链接时,会出现一个confirmDialog对话框,其中应该调用backing bean中的“delete”方法。不幸的是,这个方法没有被调用,对话框就隐藏了。因为我用调试器看到了链接的参数。PrimeFaces在confirmDialog中的commandButton不调用backing bean

这里是我的网页:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:p="http://primefaces.org/ui"> 
<h:head><title>Manage users</title></h:head> 
<h:body> 
<ui:composition template="../../template/commonLayout.xhtml"> 
    <ui:define name="content"> 
     <p:layoutUnit position="center"> 
      <h:form id="datatable"> 
       <p:tabView> 
        <p:tab title="Users"> 
         <p:button value = "Add user" outcome="addUser.xhtml?faces-redirect=true"/> 
         <p:growl id="messages" showDetail="true"/> 
         <p:dataTable id="usersTable" var="user" value="#{userMB.userList}" paginator="true" rows="25" 
            paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"> 
          <p:ajax event="rowEdit" listener="#{userMB.onEditUser}" /> 
          <p:column headerText="First name"> 
           <h:outputText value="#{user.firstName}" /> 
          </p:column> 
          <p:column headerText="Last name"> 
           <h:outputText value="#{user.lastName}" /> 
          </p:column> 
          <p:column headerText="E-mail"> 
           <h:outputText value="#{user.email}" /> 
          </p:column> 
          <p:column headerText="Username"> 
           <h:outputText value="#{user.username}"/> 
          </p:column> 
          <p:column headerText="Hire date"> 
           <h:outputText value="#{user.hireDate}"> 
            <f:convertDateTime type="date" pattern="dd.MM.yyyy"/> 
           </h:outputText> 
          </p:column> 
          <p:column headerText="Working hours"> 
           <h:outputText value="#{user.workingHours}" /> 
          </p:column> 
          <p:column headerText="Vacation days"> 
           <h:outputText value="#{user.vacationDays}"/> 
          </p:column> 

          <p:column> 
           <h:link outcome="addUser" value="Edit"> 
            <f:param name="userID" value="#{user.id}" /> 
           </h:link> 
          </p:column> 
          <p:column> 
           <p:commandLink value="Delete" onclick="deleteConfirmDlg.show()"> 
            <f:setPropertyActionListener value="#{user}" target="#{userMB.currentUser}" /> 
           </p:commandLink> 
          </p:column>        
         </p:dataTable> 

         <p:confirmDialog id="deleteConfirmDialog" message="Are you sure?" header="Delete user" severity="alert" widgetVar="deleteConfirmDlg" appendToBody="true"> 
          <p:commandButton id="confirmDelete" value="Yes" oncomplete="deleteConfirmDlg.hide()" update="usersTable" actionListener="#{userMB.deleteUser}"/> 
          <p:commandButton id="declineDelete" value="No" onclick="deleteConfirmDlg.hide()" type="button"/> 
         </p:confirmDialog> 
        </p:tab> 
        <p:tab title="Teams"> 
         <p:button value = "Add team" outcome="addTeam.xhtml?faces-redirect=true"/> 
         <p:dataTable var="team" value="#{userMB.teamList}" paginator="true" rows="25" 
          paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"> 
          <p:column headerText="Name"> 
           <h:outputText value="#{team.name}" /> 
          </p:column> 
          <p:column headerText="Team leader"> 
           <h:outputText value="#{team.teamLeader.displayName}" /> 
          </p:column> 
         </p:dataTable> 
        </p:tab> 
       </p:tabView> 
      </h:form> 
    </p:layoutUnit> 
    </ui:define> 
</ui:composition> 

支持bean的方法:

public void deleteUser(ActionEvent event) { 
    if (null != currentUser) { 
     // check if userID is set and delete 
     System.out.println("delete user with id " + currentUser.getId()); 
     userService.deleteUser(currentUser); 
     currentUser = new User(); 
    } 
} 

我检查,我没有嵌套形式,因为这是对最常见的情况支持豆不被称为...

这里是我的模板fil ES:

CommonLayout.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:p="http://primefaces.org/ui"> 
<h:head> 
<h:outputStylesheet library="css" name="default.css" /> 
</h:head> 
<h:body> 
<p:layout fullPage="true"> 
<div id ="header"> 
<ui:insert name="header"> 
    <ui:include src="commonHeader.xhtml"></ui:include> 
</ui:insert> 
</div> 
<div id="left-menu"> 
<ui:insert name="left-menu"> 
    <ui:include src="leftMenu.xhtml"></ui:include> 
</ui:insert> 
</div> 
<div id ="content"> 
<ui:insert name="content"> 
    <ui:include src="commonContent.xhtml"></ui:include> 
</ui:insert> 
</div> 
</p:layout> 
</h:body> 
</html> 

CommonHeader.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head> 
</h:head> 
<h:body> 
    <ui:composition> 
     <p:layoutUnit position="north" size="100"> 
      <h:outputText value="Welcome #{request.remoteUser}!"/> <br /> 
      <a href="#{request.contextPath}/j_spring_security_logout">Logout</a><br /> 
     </p:layoutUnit> 
    </ui:composition> 
</h:body> 
</html> 

CommonContent.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head> 
</h:head> 
<h:body> 
    <ui:composition> 
     <p:layoutUnit position="center"> 
       <ui:include src="../widgets/schedule.xhtml"/> 
     </p:layoutUnit> 
    </ui:composition> 
</h:body> 
</html> 

LeftMenu.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head> 
</h:head> 
<h:body> 
    <ui:composition> 
     <p:layoutUnit position="west" size="200"> 
     <h:form> 
      <p:menu width="100%" rendered="#{request.isUserInRole('ROLE_MANAGER')}"> 
       <p:submenu label="Management"> 
        <p:menuitem value="Manage users" outcome="/pages/management/manageUsers.xhtml?faces-redirect=true"></p:menuitem> 
       </p:submenu> 
      </p:menu> 
      </h:form> 
    </p:layoutUnit> 
    </ui:composition> 
</h:body> 
</html> 

我使用PrimeFaces 3.4和JSF 2.0

+2

对不起,我甚至都没看完你的整篇文章。这些代码/文本是否真的有必要?干杯! – SimonSez

+1

确实存在很多代码噪声(尽量减少将来的问题),但幸运的是,所描述的症状对我来说足以理解/猜测问题的原因,而不需要查看代码:) – BalusC

+0

对不起,我想还包括我的模板文件,以确保其中没有其他嵌套表单。 – AndaP

回答

19

<p:confirmDialog ... appendToBody="true">将导致确认对话框的HTML表示,以由JavaScript DOM期间被重定位到HTML <body>元件的端准备。

但是,这会导致确认对话框的HTML表示不再以任何形式存在。所以实际上没有任何表单被提交,也没有发送请求参数,因此JSF将无法识别该操作。

您需要给自己的确认对话框<h:form>

<p:confirmDialog ...> 
    <h:form> 
     ... 
    </h:form> 
</p:confirmDialog> 

,避免维护过程中的困惑(嵌套形式即是非法的),我也建议该组件移动到模板的结束,至少“外” <h:form>后。

+0

我在表单之后移动了对话框,现在不幸的是,这个update =“:datatable:usersTable”不能用作组件si找不到的。我认为这是引用表单之外的组件的方式。 – AndaP

+0

''也是一个命名容器。给它一个固定的ID,就像''并使用'update =“:datatable:tabs:usersTable”'。另请参阅http://stackoverflow.com/questions/8634156/how-to-reference-components-in-jsf-ajax-cannot-find-component-with-identifier/8644762#8644762 – BalusC

+0

也向添加了一个ID,并且它现在有效。非常感谢! – AndaP

相关问题