1

我正在使用 primefaces3.3 版本。我有一个数据表来显示详细信息,并将 CRUD 按钮作为数据表页脚的一部分。在数据表中选择特定行后,单击 CRUD 按钮将显示一个对话框。

此对话框包含具有 requiredMessage 和 validationMessage 属性的输入字段以及保存按钮。当我不输入任何输入并单击对话框中的保存按钮时,将显示验证消息。但是,关闭对话框后,即使在数据表中选择了不同的行,也只会显示旧数据。我已经附加了 BackingBean 和 xhtml 代码。

帐户.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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
 <h:head>
 </h:head>
 <h:body>
 <ui:composition template="/template.xhtml">
    <ui:define name="body">
        <h:form id="form" prependId="false">
            <p:contextMenu for="dataTable">
                <p:menuitem value="View" update=":form2:accountDisplay"
                    icon="ui-icon-search" oncomplete="accountDialog.show()"
                    process="@this">
                    <f:setPropertyActionListener value="#{account}"
                        target="#{accountBean.selectedAccount}" />
                </p:menuitem>
                <p:menuitem value="Edit" update=":form2:accountDisplay"
                    icon="ui-icon-edit" oncomplete="accountDialog.show()"
                    process="@this" immedeate="true">
                    <f:setPropertyActionListener value="#{account}"
                        target="#{accountBean.currentAccount}" />
                </p:menuitem>
                <p:menuitem value="Delete" update=":dialogForm:confirmDialog"
                    icon="ui-icon-close" oncomplete="confirmation.show()" />
            </p:contextMenu>

            <h:panelGroup id="dataPanel">
                <p:dataTable var="account" value="#{accountBean.accounts}"
                    paginator="true" rows="20" rowKey="#{account.accountCode}"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    selection="#{accountBean.selectedAccount}" selectionMode="single"
                    id="dataTable">
                    <p:column id="accountCode" headerText=" Account Code"
                        sortBy="#{account.accountCode}" filterBy="#{account.accountCode}"
                        filterMatchMode="startsWith" footerText="startsWith">
                        <h:outputText value="#{account.accountCode}" />
                    </p:column>
                    <p:column id="accountCode" headerText=" Account Code"
                        sortBy="#{account.accountDescription}" filterBy="#{account.accountDescription}"
                        filterMatchMode="startsWith" footerText="startsWith">
                        <h:outputText value="#{account.accountDescription}" />
                    </p:column>
                <f:facet name="footer">
                    <p:toolbar>
                    <p:toolbarGroup align="left">
                                    <p:commandButton id="btnAdd" value="Add" title="Add New Account"
                            icon="ui-icon-add"
                            oncomplete="accountDialog.show()" />                        
                        <p:commandButton id="editButton" value="Edit" icon="ui-icon-edit"
                            update=":form2:accountDisplay" oncomplete="accountDialog.show()">

                        </p:commandButton>
                        <p:commandButton id="showDialogButton" icon="ui-icon-delete"
                            value="Delete"
                            title="Delete Account Details" oncomplete="confirmation.show()">
                        </p:commandButton>
                        <p:commandButton id="viewButton" value="View"
                            icon="ui-icon-search" update=":form2:display"
                            oncomplete="accountDialog.show()" />
                        <p:commandButton id="btnRefresh" styleClass="refreshButton"
                            title="Refresh Account Information" value="Refresh"
                            icon="ui-icon-refresh"
                            actionListener="#{accountBean.populateAccounts}"
                            update=":form:dataTable"/>                          
                    </p:toolbarGroup>
                    </p:toolbar>    
                    </f:facet>
                </p:dataTable>

            </h:panelGroup>
        </h:form>
        <h:form id="form2">
            <!--  <p:growl id="growl" showDetail="true" sticky="true" /> -->   
            <p:dialog id="basicDialog" header="Account Details"
                widgetVar="detailDialog" resizable="false">

                <h:panelGrid id="display" columns="2">
                    <h:outputText value="#{bundle.AccountLabel_accountCode}" />
                    <h:outputText value="#{accountBean.selectedAccount.accountCode}"
                        title="#{bundle.AccountTitle_accountCode}" />

                    <h:outputText value="#{bundle.AccountLabel_accountDescription}" />
                    <h:outputText value="#{accountBean.selectedAccount.accountDescription}"
                        title="#{bundle.AccountTitle_alertCode}" />
                </h:panelGrid>
            </p:dialog>
            <p:dialog header="Account Detail" widgetVar="accountDialog"
                resizable="false" height="600" width="600" showEffect="clip"
                hideEffect="fold" id="dialog" modal="true">

                <h:panelGrid id="accountDisplay" columns="2" cellpadding="4">

                    <f:facet name="header">
                        <p:messages id="messages" autoUpdate="true" />
                    </f:facet>

                    <h:outputLabel value="Account Code:" for="accountCode" />
                    <p:inputText label="Account Code" id="accountCode"
                        value="#{accountBean.selectedAccount.accountCode}"
                        title="Account Code" required="true"
                        validatorMessage="#{bundle.AccountValidationAccountCode}" requiredMessage="#{bundle.AccountRequiredMessage_accountCode}">

                    </p:inputText>
                    <h:outputLabel value="Account Description:" for="accountDescripition" />
                    <p:inputText label="Account Description" id="accountDescription"
                        value="#{accountBean.selectedAccount.accountDescription}"
                        title="Account Description" required="true"
                        validatorMessage="#{bundle.AccountValidationAccountDescription}" requiredMessage="#{bundle.AccountRequiredMessage_accountDescription}">

                    </p:inputText>
                    <f:facet name="footer">
                        <!-- <p:commandButton value="New" icon="ui-icon-check"/> -->
                        <h:outputLabel />
                        <p:commandButton styleClass="saveButton" value="Save"
                            icon="ui-icon-check" 
                            actionListener="#{accountBean.createOrUpdate}" 
                            update=":form:dataTable,:form2:accountDisplay" oncomplete="if (!args.validationFailed) {accountDialog.hide();}"/>

                        <p:commandButton styleClass="cancelButton" value="Cancel"
                            icon="ui-icon-cancel" oncomplete="accountDialog.hide()" process="@this"/>

                    </f:facet>
                </h:panelGrid>
            </p:dialog>

        </h:form>

        <h:form id="dialogForm">
            <p:confirmDialog id="confirmDialog" header="Confirm Delete Account"
                severity="alert" widgetVar="confirmation">
                <f:facet name="message">
                    <h:outputText
                        value="Delete #{accountBean.selectedAccount.accountCode}?" />
                </f:facet>

                <p:commandButton id="confirm" value="Yes"
                    oncomplete="confirmation.hide()" update=":form:dataTable"
                    actionListener="#{accountBean.remove}" ajax="false"/>
                <p:commandButton id="decline" value="No"
                    onclick="confirmation.hide()" type="button" ajax="false"/>
            </p:confirmDialog>
        </h:form>
    </ui:define>
</ui:composition>

AccountBean.java

   package com.cba.web.beans;

   import java.io.Serializable;
   import java.util.List;

   import javax.faces.bean.ManagedBean;
   import javax.faces.bean.ViewScoped;
   import javax.faces.context.FacesContext;
   import javax.faces.event.ActionEvent;

   import org.primefaces.context.RequestContext;

   import com.cba.services.AccountService;
   import com.cba.utils.ServiceLocator;
   import com.cba.web.model.Account;

   @ManagedBean(name = "accountBean")
   @ViewScoped
   public class AccountBean implements Serializable {

   private transient AccountService accountService;

   private List<Account> accounts;

   private Account selectedAccount = new Account();

private Account currentAccount = new Account();

public Account getSelectedAccount() {
    return selectedAccount;
}

public void setSelectedAccount(Account selectedAccount) {
    this.selectedAccount = selectedAccount;
}

public Account getCurrentAccount() {
    return currentAccount;
}

public void setCurrentAccount(Account currentAccount) {
    this.currentAccount = currentAccount;
}

public AccountBean() {

    populateAccounts();
}

public AccountService getAccountService() {
    return accountService;
}

public void setAccountService(AccountService accountService) {
    this.accountService = accountService;
}

public List<Account> getAccounts() {
    return accounts;
}

public void populateAccounts() {
    accountService = (AccountService) ServiceLocator.getInstance().getBean(
            "accountService");
    accounts = accountService.getAccountDetails();
}

public void createOrUpdate(ActionEvent e) {
    try {

        System.out.println("entering createOrUpdate in AccountBean");
        accountService = (AccountService) ServiceLocator.getInstance()
                .getBean("accountService");
        accountService.updateAccountDetails(selectedAccount);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void remove(ActionEvent e) {
    try {
        accountService = (AccountService) ServiceLocator.getInstance()
                .getBean("accountService");
        accountService.deleteAccountDetails(selectedAccount);
        selectedAccount = new Account();
        populateAccounts();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

我正在使用 Google Chrome 19 和 IE 8。对此的任何输入将不胜感激。提前致谢

4

0 回答 0