对话框的保存按钮不是触发bean方法

时间:2014-01-27 14:11:05

标签: jsf jsf-2 primefaces dialog

我正在尝试使用从数据表加载行数据的对话框来实现编辑功能。我可以将数据表中的人员详细信息加载到对话框中并相应地显示保存按钮。但我的保存按钮没有触发我的bean方法来保存对话框中的更新记录。我可能会遗漏,这是我的代码,

  1. .xhtml文件

    <html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:h="http://xmlns.jcp.org/jsf/html" 
        xmlns:p="http://primefaces.org/ui" 
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
        xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>Asset Manager-Home</title>
        </h:head>
        <h:body>
            <p:layout fullPage="true">
    
                <p:layoutUnit position="north" size="200">
                    <h:outputText value="Home content." />
                </p:layoutUnit>
    
                <p:layoutUnit position="south" size="50">
                    <h:outputText value="All Rights Reserved." />
                </p:layoutUnit>
    
                <p:layoutUnit position="west" size="250" header="">
                    <h:form>
                        <p:panelMenu>
                            <p:submenu label="Home">
                                <p:menuitem value="Home" url="/faces/index.xhtml" />
                            </p:submenu>
                            <p:submenu label="Staff">
                                <p:menuitem value="Create Staff " url="/face/createStaff.xhtml" icon="ui-icon-disk" />
                                <p:menuitem value="Update Staff " url="/faces/updateStaff.xhtml" />
                                <p:menuitem value="All Staff " url="/faces/updateStaff.xhtml" />
                            </p:submenu>
                            <p:submenu label="Categories">
                                <p:menuitem value="Create Category" url="/faces/createCategory.xhtml" icon="ui-icon-disk" />
                                <p:menuitem value="Update Category" url="/faces/updateCategory.xhtml" />
                                <p:menuitem value="View Category" url="/faces/updateCategory.xhtml" />
                            </p:submenu>
                            <p:submenu label="Asset">
                                <p:menuitem value="Create Asset" url="/faces/createAsset.xhtml" icon="ui-icon-disk" />
                                <p:menuitem value="Update Asset" url="/faces/updateAsset.xhtml" />
                                <p:menuitem value="All Asset" url="/faces/updateAsset.xhtml" />
                            </p:submenu>
                            <p:submenu label="Asset Status">
                                <p:menuitem value="Create/Update Status Register" url="/faces/CreateStatus.xhtml" />
                            </p:submenu>
                            <p:submenu label="Assigned Asset">
                                <p:menuitem value="Assign Asset" url="/faces/assignAsset.xhtml" icon="ui-icon-disk" />
                                <p:menuitem value="Do Update" url="/faces/updateAssignAsset.xhtml" />
                                <p:menuitem value="View All" url="/faces/updateAssignAsset.xhtml" />
                            </p:submenu>
                            <p:submenu label="Reports">
    
                            </p:submenu>
                        </p:panelMenu>
                    </h:form>
                </p:layoutUnit>
    
                <p:layoutUnit position="east" size="250" header="Right">
                    <h:outputText value="Right unit content." />
                </p:layoutUnit>
    
                <p:layoutUnit position="center">
    
                    <p:separator id="customSeparator" style="width:1000px;height:20px" />
                    <p:panel header="All Staff" id="allstaff">
    
                        <h:form prependId="false" id="staffform">
    
                            <p:dataTable id="dataTable" var="stf" value="#{allStaffTableBean.staffList}" 
                                paginator="true" rows="10" 
                                paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                                rowsPerPageTemplate="5,10,15" selectionMode="single" 
                                selection="#{allStaffTableBean.selectedStaff}" 
                                emptyMessage="KoblaGasu:: No data to be found for displaying" 
                                rowKey="#{stf.uniqueID}">
    
    
                                <p:column sortBy="#{stf.uniqueID}" filterBy="#{stf.uniqueID}">
    
                                    <f:facet name="header">
                                        <h:outputText value="Staff ID" />
                                    </f:facet>
    
                                    <h:outputText value="#{stf.uniqueID}" />
                                </p:column>
    
                                <p:column sortBy="#{stf.firstName}" filterBy="#{stf.firstName}">
                                    <f:facet name="header">
                                        <h:outputText value="First Name" />
                                    </f:facet>
                                    <h:outputText value="#{stf.firstName}" />
                                </p:column>
    
                                <p:column sortBy="#{stf.lastName}" filterBy="#{stf.lastName}">
                                    <f:facet name="header">
                                        <h:outputText value="Last Name" />
                                    </f:facet>
                                    <h:outputText value="#{stf.lastName}" />
                                </p:column>
    
                                <f:facet name="footer">
                                    <div>
                                        <p:commandButton id="edit" value="Edit" update=":staffform:display" onclick="PF('StaffEditDialog').show()" />
                                        <p:commandButton id="delet" value="Delete" />
                                    </div>
                                </f:facet>
                            </p:dataTable>
    
                            <p:dialog id="StaffEditDlg" widgetVar="StaffEditDialog" modal="true" resizable="false" appendTo="@(body)" header="Edit Staff" closeOnEscape="true">
                                <h:panelGroup id="display">
                                    <p:panelGrid columns="2" rendered="#{allStaffTableBean.selectedStaff != null}">
                                        <h:outputLabel value="Staff ID" for="staffUniqueID" />
                                        <h:outputText id="staffUniqueID" value="#{allStaffTableBean.selectedStaff.uniqueID}" />
    
                                        <p:outputLabel value="First Name" for="firstName" />
                                        <p:inputText id="firstName" value="#{allStaffTableBean.selectedStaff.firstName}" title="First Name" size="45" maxlength="45" />
    
                                        <p:outputLabel value="Last Name" for="lastName" />
                                        <p:inputText id="lastName" value="#{allStaffTableBean.selectedStaff.lastName}" title="Last Name" size="45" maxlength="45" />
                                    </p:panelGrid>
    
                                    <p:commandButton actionListener="#{allStaffTableBean.update}" value="Save" oncomplete="PF().hide();" />
                                    <p:commandButton value="Cancel" onclick="StaffEditDialog.hide()" />
                                </h:panelGroup>
                            </p:dialog>
                        </h:form>
                    </p:panel>
                </p:layoutUnit>
            </p:layout>
        </h:body>
    </html>
    
  2. 员工豆

    public class Staff implements Serializable
    {
    
        private String uniqueID;
        private String firstName;
        private String lastName;
    
        /**
         * Creates a new instance of Staff
         */
        public Staff()
        {
    
        }
    
        public Staff(String uniqueID, String firstName, String lastName)
        {
            this.uniqueID = uniqueID;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public void updateSaff(ActionEvent event)
        {
            boolean status = new StaffManager().updateStaff(uniqueID, firstName, lastName);
            System.out.println("Kobla::Update called in Staff.java");
        }
    
        /**
         * @ return string's
         */
    
        public String save()
        {
            StaffManager staffManager = new StaffManager();
            boolean status = staffManager.saveStaff(uniqueID, firstName, lastName);
    
            if(status == true)
            {
    
                return "index";
            }
            else
            {
                return "createStaff";
            }
        }
    
        /**
         * @return: method to call getSstaff for staff manager
         */
        public String searchStaff()
        {
            Staff myStaff = new Staff();
    
            myStaff = new StaffManager().getStaff(uniqueID);
    
            this.uniqueID = myStaff.getUniqueID();
            this.firstName = myStaff.getFirstName();
            this.lastName = myStaff.getLastName();
    
            return "updateStaff";
        }
    
        public String deleteStaff()
        {
            StaffManager staffManager = new StaffManager();
            boolean status = staffManager.delStaff(uniqueID);
    
            if(status == true)
            {
                return "index";
            }
            else
            {
                return "updateStaff";
            }
        }
    
        /**
         * @return the uniqueID
         */
        public String getUniqueID()
        {
            return uniqueID;
        }
    
        /**
         * @param uniqueID
         *            the uniqueID to set
         */
        public void setUniqueID(String uniqueID)
        {
            this.uniqueID = uniqueID;
        }
    
        /**
         * @return the firstName
         */
        public String getFirstName()
        {
            return firstName;
        }
    
        /**
         * @param firstName
         *      the firstName to set
         */
        public void setFirstName(String firstName)
        {
            this.firstName = firstName;
        }
    
        /**
         * @return the lastName
         */
        public String getLastName()
        {
            return lastName;
        }
    
        /**
         * @param lastName
         *      the lastName to set
         */
        public void setLastName(String lastName)
        {
            this.lastName = lastName;
        }
    
        @Override
        public String toString()
        {
            return "Staff{" + "uniqueID=" + uniqueID + ", firstName=" + firstName + ", lastName=" + lastName + '}';
        }
    }
    
  3. 还有一个dataTable bean

    public class AllStaffTableBean implements Serializable
    {
        private List<Staff> staffList;
        private Staff selectedStaff;
    
        /**
         * Creates a new instance of AllStaffTableBean
         */
        public AllStaffTableBean()
        {
            staffList = new ArrayList<>();
    
            staffList = new StaffManager().getAllStaff();
    
            for(Staff stf : staffList)
            {
                System.out.println("allstafftablebean:: " + stf);
            }
            {
                System.out.println("selectedStaff :: " + selectedStaff);
            }
        }
    
        public void update(ActionEvent event)
        {
            RequestContext context = RequestContext.getCurrentInstance();
    
            new StaffManager().saveStaff(selectedStaff.getUniqueID(), selectedStaff.getFirstName(), selectedStaff.getLastName());
            System.out.println("Update callded in allstafftablebean.java :: " + selectedStaff);
        }
    
        /**
         * @return the staffList
         */
        public List<Staff> getStaffList()
        {
            return staffList;
        }
    
        /**
         * @return the selectedStaff
         */
        public Staff getSelectedStaff()
        {
            return selectedStaff;
        }
    
        /**
         * @param selectedStaff
         *            the selectedStaff to set
         */
        public void setSelectedStaff(Staff selectedStaff)
        {
            this.selectedStaff = selectedStaff;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

你必须告诉commandButton你想要处理什么,试试:

 <p:commandButton actionListener 
  ="#{allStaffTableBean.update}" value="Save"   oncomplete="PF().hide();"
  process="@this,StaffEditDlg"/>

或:

<p:commandButton actionListener 
  ="#{allStaffTableBean.update}" value="Save"   oncomplete="PF().hide();"
  process="@this,@widgetVar(StaffEditDialog)"/>

要处理对话框中的数据,您必须处理dataTable信息(所选项目),以便:

<p:commandButton id="edit" value="Edit" update=":staffform:display"   
onclick="PF('StaffEditDialog').show()" process=":staffform:dataTable" />

我建议使用另一个Staff编辑实例,记住实例只是引用,所以如果更改值(因任何原因),将更新为具有该引用的所有变量,查找Shadow clone或deep克隆。我认为使用这些代码将会起作用。如果没有,请添加评论。

玩得开心!