在插入或更新数据时自动更新整个DataTable

时间:2017-11-16 08:07:11

标签: jsf primefaces jsf-2 datatable

插入或更新行数据后,我很难渲染PrimeFaces Datatable。打开时,它不会在数据表或编辑对话框中更新。它显示较旧的值。当服务器停止并启动应用程序时,它会在数据表或编辑对话框中显示新值。 我正在将它从primefaces 2.2.1迁移到primefaces 6.1。

这是更新的JSF页面:

<h:form id="dbform">
<p:growl id="growl" />
<h:panelGrid>
    <p:commandButton value="ADD New Data"
        onclick="PF('dlg1').show();" render="tbl"
        update="dialogfrom:newdevice" icon="ui-icon-plusthick"
        immediate="true" />
</h:panelGrid>
<h:panelGroup id="dbpanelgrid">
    <p:dataTable style="width:800px;" id="tbl" var="med"
        value="#{seniorAction.searchResults}" paginator="true"
        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
        rowsPerPageTemplate="5,10,15" rowKey="#{med.empNo}"
        selectionMode="single"
        selection="#{seniorAction.selectedData}"
        rowClasses="table-evenRow,table-oddRow" rows="5"
        rowSelectListener="#{seniorAction.onDataSelect}" filteredValue="#{seniorAction.filteredEmployeeList}">
        <p:column sortBy="#{med.empNo}" filterBy="#{med.empNo}"
            filterMatchMode="contains">
            <f:facet name="header">
                <h:outputText value="EMPNO." />
            </f:facet>
            <h:outputText value="#{med.empNo}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="EMP TYPE " />
            </f:facet>
            <h:outputText value="#{med.empType}"
                styleClass="capitalized" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="CATEGORY TYPE " />
            </f:facet>
            <h:outputText value="#{med.catgryType}"
                styleClass="capitalized" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="DOCA" />
            </f:facet>
            <h:outputText value="#{med.doca}" />
        </p:column>
        <p:column style="text-align:center;">
            <f:facet name="header">
                <h:outputText value="EDIT" />
            </f:facet>
            <p:commandButton title="EDIT"
                onclick="PF('dlg2').show();" render="tbl"
                action="#{seniorAction.editEmpNoDetails}"
                icon="ui-icon-pencil" update="editfrom:editdevice"
                immediate="true">
                <f:setPropertyActionListener value="#{med}"
                    target="#{seniorAction.selectedData}" />
            </p:commandButton>
        </p:column>
        <p:column style="text-align:center;">
            <f:facet name="header">
                <h:outputText value="DELETE" />
            </f:facet>
            <p:commandButton update="@form,dbform:tbl"
                title="DELETE" action="#{seniorAction.deleteEmpNo}"
                render="tbl" icon="ui-icon-trash" immediate="true">
                <f:setPropertyActionListener value="#{med}"
                    target="#{seniorAction.selectedData}" />
            </p:commandButton>
        </p:column>
    </p:dataTable>
    <!-- Delete Dialog box -->
    <!-- Preview Dialog box -->
</h:panelGroup>
</h:form>
<p:growl id="growl" />
<p:dialog id="basicDialog" header="ADD New Data"
modal="true" widgetVar="dlg1" width="960" height="500"
hideEffect="explode" resizable="false">
<h:form id="dialogfrom">
    <h:panelGrid columns="4" cellpadding="5" id="newdevice">
        <h:outputLabel for="empno" value="EMPNO *" />
        <p:inputText value="#{seniorAction.empNo.empNo}"
            id="empno" required="true" maxlength="4" size="4">
            <f:validateLength maximum="4" />
        </p:inputText>
        <h:outputLabel for="name" value="NAME *" />
        <p:inputText value="#{seniorAction.empNo.name}"
            id="name" required="true" styleClass="capitalized"
            maxlength="40" size="40">
            <f:validateLength maximum="40" />
        </p:inputText>
        <h:outputLabel for="gender" value="GENDER *" />
        <h:selectOneRadio id="gender"
            value="#{seniorAction.empNo.gender}">
            <f:selectItem itemLabel="Male" itemValue="MALE" />
            <f:selectItem itemLabel="Female" itemValue="FEMALE" />
        </h:selectOneRadio>
        <h:outputLabel for="caste" value="CATEGORY *" />
        <h:selectOneMenu id="caste"
            value="#{seniorAction.empNo.casteCatgry}"
            required="true">
            <f:selectItem itemLabel="--Select Category--"
                noSelectionOption="true" />
            <f:selectItem itemValue="UR" itemLabel="UR" />
            <f:selectItem itemValue="OBC" itemLabel="OBC" />
            <f:selectItem itemValue="SC" itemLabel="SC" />
            <f:selectItem itemValue="ST" itemLabel="ST" />
            <f:selectItem itemValue="ESM" itemLabel="ESM" />
            <f:selectItem itemValue="PH/VH" itemLabel="PH/VH" />
            <f:selectItem itemValue="PH/OH" itemLabel="PH/OH" />
            <f:selectItem itemValue="PH/HH" itemLabel="PH/HH" />
            <f:selectItem itemValue="PH/VH/OBC"
                itemLabel="PH/VH/OBC" />
            <f:selectItem itemValue="PH/OH/OBC"
                itemLabel="PH/OH/OBC" />
            <f:selectItem itemValue="PH/HH/OBC"
                itemLabel="PH/HH/OBC" />
            <f:selectItem itemValue="PH/VH/SC"
                itemLabel="PH/VH/SC" />
            <f:selectItem itemValue="PH/VH/ST"
                itemLabel="PH/VH/ST" />
            <f:selectItem itemValue="PH/OH/SC"
                itemLabel="PH/OH/SC" />
            <f:selectItem itemValue="PH/OH/ST"
                itemLabel="PH/OH/ST" />
            <f:selectItem itemValue="PH/HH/SC"
                itemLabel="PH/HH/SC" />
            <f:selectItem itemValue="PH/HH/ST"
                itemLabel="PH/HH/ST" />
        </h:selectOneMenu>
        <h:outputLabel for="dob" value="DOB *" />
        <p:calendar id="dob" readonlyInput="true"
            value="#{seniorAction.dob}"
            maxdate="#{seniorAction.today}" pattern="dd-MM-yyyy"
            showOn="button" required="true" navigator="true">
            <p:ajax event="dateSelect" update="doj" />
        </p:calendar>
        <h:outputLabel for="desg" value="DESGINATION *" />
        <p:inputText value="#{seniorAction.empNo.desg}"
            id="desg" required="true" styleClass="capitalized"
            maxlength="40" size="40">
            <f:validateLength maximum="40" />
        </p:inputText>
        <h:outputLabel for="desgtype" value="DESG TYPE *" />
        <h:selectOneRadio id="desgtype"
            value="#{seniorAction.empNo.empType}">
            <f:selectItem itemLabel="Officers"
                itemValue="OFFICERS" />
            <f:selectItem itemLabel="Staff" itemValue="STAFF" />
        </h:selectOneRadio>
        <h:outputLabel for="emptype" value="EMP TYPE *" />
        <h:selectOneRadio id="emptype"
            value="#{seniorAction.empNo.catName}">
            <f:selectItem itemLabel="General" itemValue="GENERAL" />
            <f:selectItem itemLabel="Finance" itemValue="FINANCE" />
            <f:selectItem itemLabel="Staff" itemValue="" />
        </h:selectOneRadio>
        <h:outputLabel for="seniority" value="SENIORITY *" />
        <p:inputText value="#{seniorAction.empNo.senioritySno}"
            id="seniority" required="true" maxlength="4" size="4">
            <f:validateLength maximum="4" />
        </p:inputText>
        <h:outputLabel for="senioritytype"
            value="SENIORITY BY TYPE *" />
        <h:selectOneMenu id="senioritytype" immediate="true"
            value="#{seniorAction.empNo.catgryType}">
            <f:selectItem itemLabel="Select One" itemValue="all" />
            <f:selectItems id="options1"
                value="#{seniorAction.typeNames}" />
        </h:selectOneMenu>
        <h:outputLabel for="qual" value="QUALIFICATION *" />
        <p:inputTextarea value="#{seniorAction.empNo.qual}"
            id="qual" required="true" maxlength="200" rows="5"
            cols="40" autoResize="false" style="resize: none">
            <f:validateLength maximum="200" />
        </p:inputTextarea>
        <h:outputLabel for="doj" value="DOJ *" />
        <p:calendar id="doj" readonlyInput="true"
            value="#{seniorAction.doj}"
            mindate="#{seniorAction.dob}" pattern="dd-MM-yyyy"
            showOn="button" required="true" navigator="true">
            <p:ajax event="dateSelect" />
        </p:calendar>
        <h:outputLabel for="doca" value="DOCA *" />
        <p:calendar id="doca"
            value="#{seniorAction.empNo.doca}"
            maxdate="#{seniorAction.today}" pattern="dd-MM-yyyy"
            showOn="button" required="true" navigator="true" />
        <h:outputLabel for="branch" value="BRANCH *" />
        <p:inputText value="#{seniorAction.empNo.branch}"
            id="branch" required="true" maxlength="30" size="30"
            styleClass="capitalized">
            <f:validateLength maximum="30" />
        </p:inputText>
        <h:outputLabel for="payscale" value="PAYSCALE *" />
        <h:selectOneMenu id="payscale" immediate="true"
            value="#{seniorAction.empNo.payscale}">
            <f:selectItem itemLabel="Select One" itemValue="all" />
            <f:selectItems id="options2"
                value="#{seniorAction.diffPayScales}" />
        </h:selectOneMenu>
        <h:outputLabel for="otherdes" value="OTHER DES *" />
        <p:inputText value="#{seniorAction.empNo.otherDes}"
            id="otherdes" maxlength="30" size="30"
            styleClass="capitalized">
            <f:validateLength maximum="30" />
        </p:inputText>
        <f:facet name="footer">
            <p:commandButton id="confirm" value="Submit"
                update="@form,dbform:tbl" 
                actionListener="#{seniorAction.dataInsertion}"
                oncomplete="handleDialog(xhr, status, args)"
                onclick="javascript:void(0)" icon="ui-icon-disk" />
            <p:commandButton id="cancel" value="Cancel"
                update=":dialogfrom" onclick="PF('dlg1').hide();"
                type="reset" icon="ui-icon-close" />
        </f:facet>
    </h:panelGrid>
</h:form>
</p:dialog>
<script type="text/javascript">
function handleDialog(
        xhr,
        status,
        args) {
    if (args.validationFailed) {
        jQuery(
                '#basicDialog')
                .effect(
                        "shake",
                        {
                            times : 3
                        },
                        100);
    } else {
        PF('dlg1')
                .hide();
    }
}
</script>
<p:growl id="editgrowl" />
<p:dialog id="editDialog"
header="Edit Master Data Details" widgetVar="dlg2"
modal="true" width="480" height="620"
hideEffect="explode" resizable="false">
<h:form id="editfrom">
    <h:panelGrid columns="2" cellpadding="5" id="editdevice">
        <h:outputLabel for="empno" value="EMP NO" />
        <p:inputText id="empno"
            value="#{seniorAction.selectedData.empNo}"
            readonly="true" size="4">
        </p:inputText>
        <h:outputLabel for="name" value="NAME" />
        <p:inputText id="name"
            value="#{seniorAction.selectedData.name}"
            maxlength="40" size="40">
        </p:inputText>
        <h:outputLabel for="desg" value="DESGINATION" />
        <p:inputText id="desg"
            value="#{seniorAction.selectedData.desg}"
            maxlength="40" size="40">
        </p:inputText>
        <h:outputLabel for="empType" value="EMP TYPE" />
        <h:selectOneMenu id="empType" immediate="true"
            value="#{seniorAction.selectedData.empType}">
            <f:selectItem itemLabel="Select One" itemValue="all" />
            <f:selectItems id="options1"
                value="#{seniorAction.empTypeNames}" />
        </h:selectOneMenu>
        <h:outputLabel for="catgryType" value="CATEGORY TYPE" />
        <h:selectOneMenu id="catgryType" immediate="true"
            value="#{seniorAction.selectedData.catgryType}">
            <f:selectItem itemLabel="Select One" itemValue="all" />
            <f:selectItems id="options2"
                value="#{seniorAction.typeNames}" />
        </h:selectOneMenu>
        <h:outputLabel for="seniority" value="SENIORITY SNO" />
        <p:inputText id="seniority"
            value="#{seniorAction.selectedData.senioritySno}"
            maxlength="4" size="4" type="int">
        </p:inputText>
        <h:outputLabel for="qual" value="QUALIFICATION" />
        <p:inputTextarea id="qual"
            value="#{seniorAction.selectedData.qual}"
            maxlength="200" rows="5" cols="40" autoResize="false"
            style="resize: none">
        </p:inputTextarea>
        <h:outputLabel for="branch" value="BRANCH" />
        <p:inputText id="branch"
            value="#{seniorAction.selectedData.branch}"
            maxlength="30" size="30">
        </p:inputText>
        <h:outputLabel for="doca" value="DOCA" />
        <p:calendar id="doca"
            value="#{seniorAction.selectedData.doca}"
            pattern="dd-MM-yyyy" showOn="button" required="true"
            maxdate="#{seniorAction.selectedData.currentDate}"
            navigator="true" />
        <h:outputLabel for="payscale" value="PAYSCALE" />
        <h:selectOneMenu id="payscale" immediate="true"
            value="#{seniorAction.selectedData.payscale}">
            <f:selectItem itemLabel="Select One" itemValue="all" />
            <f:selectItems id="options3"
                value="#{seniorAction.diffPayScales}" />
        </h:selectOneMenu>
        <h:outputLabel for="otherdes" value="OTHER DES" />
        <p:inputText id="otherdes"
            value="#{seniorAction.selectedData.otherDes}"
            maxlength="30" size="30">
        </p:inputText>
        <f:facet name="footer" align="center">
            <p:commandButton id="conform" value="Update"
                actionListener="#{seniorAction.detailsModification}"
                update="dbform:tbl,:editgrowl"
                oncomplete="handleEditDialog(xhr, status, args)"
                onclick="javascript:void(0)" icon="ui-icon-disk" />
            <p:commandButton id="cancel" value="Cancel"
                update=":editgrowl" onclick="PF('dlg2').hide()"
                type="reset" icon="ui-icon-close" />
        </f:facet>
    </h:panelGrid>
</h:form>
</p:dialog>
<script type="text/javascript">
function handleEditDialog(
        xhr,
        status,
        args) {
    if (args.validationFailed) {
        jQuery(
                '#editDialog')
                .effect(
                        "shake",
                        {
                            times : 3
                        },
                        100);
    } else {
        PF('dlg2')
                .hide();
    }
}
</script>

这是支持bean:

 @ManagedBean(name = "seniorAction")
    @ApplicationScoped
    public class SeniorityAction {
        SeniorityService snService = new SeniorityService();
        private Collection<SeniorityData> searchResults;
        private SeniorityData selectedData;
        private SeniorityData empNo;
        private Collection<String> typeNames;
        private Collection<String> empTypeNames;
        private Collection<String> diffPayScales;
        private Date doj;
        private Date dob;
        private Date currentDate = new Date();

        public SeniorityAction() throws SQLException {
            searchResults = snService.getAllData();
            typeNames = snService.getTypeNames();
            empTypeNames = snService.getEmpTypeNames();
            diffPayScales = snService.getDiffPayScales();
            empNo = new SeniorityData();

            Calendar c1 = Calendar.getInstance();
            doj = c1.getTime();
            Calendar c2 = Calendar.getInstance();
            c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
            c2.set(Calendar.DAY_OF_YEAR, 1);
            dob = c2.getTime();
        }

        public Collection<SeniorityData> getSearchResults() {
            return searchResults;
        }

        public void setSearchResults(Collection<SeniorityData> searchResults)
                throws SQLException {
            this.searchResults = snService.getAllData();
        }

        public SeniorityData getSelectedData() {
            return selectedData;
        }

        public void setSelectedData(SeniorityData selectedData) {
            this.selectedData = selectedData;
        }

        public Collection<String> getTypeNames() {
            return typeNames;
        }

        public void setTypeNames(Collection<String> typeNames) {
            this.typeNames = typeNames;
        }

        public Collection<String> getEmpTypeNames() {
            return empTypeNames;
        }

        public void setEmpTypeNames(Collection<String> empTypeNames) {
            this.empTypeNames = empTypeNames;
        }

        public Collection<String> getDiffPayScales() {
            return diffPayScales;
        }

        public void setDiffPayScales(Collection<String> diffPayScales) {
            this.diffPayScales = diffPayScales;
        }

        public void deleteEmpNo() throws SQLException {
            FacesContext context = FacesContext.getCurrentInstance();
            boolean result = searchResults.remove(snService
                    .deleteEmpNo(selectedData.getEmpNo()));
            if (result == false) {
                context.addMessage("deviceID", new FacesMessage(
                        "Master data deleted successfully"));
            } else {
                context.addMessage("deviceID", new FacesMessage(
                        "Master data delete error"));
            }
        }

        public void editEmpNoDetails() throws SQLException {
            FacesContext context = FacesContext.getCurrentInstance();
            Collection<SeniorityData> result = snService
                    .editEmpNoDetails(selectedData.getEmpNo());
            if (result != null) {
                context.addMessage("deviceID", new FacesMessage(
                        "Master data updated successfully"));
            } else {
                context.addMessage("deviceID", new FacesMessage(
                        "Error in updating Master data"));
            }
        }

        public void detailsModification() throws SQLException {
            FacesContext context = FacesContext.getCurrentInstance();
            String result = snService.dataModification(selectedData);
            if (result != null) {
                context.addMessage("deviceID", new FacesMessage(
                        "Master data for employee no " + selectedData.getEmpNo()
                                + " is modified successfully"));
            } else {
                context.addMessage("deviceID", new FacesMessage(
                        "Error in Master data modification"));
            }

        }

        public void dataInsertion() {
            FacesContext context = FacesContext.getCurrentInstance();

            String result = snService.newDataInsertion(empNo);

            if (result != null) {
                context.addMessage("deviceID", new FacesMessage(
                        "Added new data successfully"));
            } else {
                context.addMessage("deviceID", new FacesMessage(
                        "Error in adding new data"));
            }
        }

        public SeniorityData getEmpNo() {
            return empNo;
        }

        public void setEmpNo(SeniorityData empNo) {
            this.empNo = empNo;
        }

        public void onDataSelect(SelectEvent event) throws SQLException {
            searchResults = snService.getAllData();
        }

        public String dataModification(SeniorityData selectedData) {
            ConnectionPool pool = ConnectionPool.getInstance();
            Connection connection = pool.getConnection();
            PreparedStatement ps = null;
            String query = "update seniority_master set name =\""
                    + selectedData.getName().toUpperCase() + "\"," + "desg =\""
                    + selectedData.getDesg().toUpperCase() + "\"," + "seniority=\""
                    + selectedData.getSenioritySno() + "\"," + "emptype=\""
                    + selectedData.getEmpType() + "\"," + "type=\""
                    + selectedData.getCatgryType() + "\"," + "qual=\""
                    + selectedData.getQual().toUpperCase() + "\"," + "branch=\""
                    + selectedData.getBranch().toUpperCase() + "\"," + "doca=\""
                    + selectedData.getDocaFormatted() + "\"," + "payscale=\""
                    + selectedData.getPayscale() + "\"," + "otherdesc=\""
                    + selectedData.getOtherDes().toUpperCase()
                    + "\" where empno = '" + selectedData.getEmpNo() + "'";
            try {
                ps = connection.prepareStatement(query);
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.closePreparedStatement(ps);
                pool.freeConnection(connection);
            }
            return query;
        }

        public Date getToday() {
            Calendar c = Calendar.getInstance();
            return c.getTime();
        }

        public void setToday(Date today) {
        }

        public Date getDoj() {
            return doj;
        }

        public void setDoj(Date doj) {
            this.doj = doj;
        }

        public Date getDob() {
            return dob;
        }

        public void setDob(Date dob) {
            this.dob = dob;
            if (doj.before(dob)) {
                doj = dob;
            }
        }

        public Date getCurrentDate() {
            return currentDate;
        }

        public void setCurrentDate(Date currentDate) {
            this.currentDate = currentDate;
        }

        public String getDojFormatted() {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.format(doj);
        }

        public String getDobFormatted() {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.format(dob);
        }
    }

在数据表中插入或删除的数据未反映。

0 个答案:

没有答案