Primefaces openDialog(dlg.xhtml)使用相同的数据重新打开,每次关闭对话框时不应该销毁viewscope bean吗?

时间:2016-04-18 16:14:24

标签: jsf jsf-2 primefaces dialog reset

我正在尝试使用Primefaces对话框在数据库中插入实体对象,它在我第一次使用对话框时工作,当我第二次尝试使用它时,在对话框中保持相同的bean,这里是我的代码:

这是在文件 index.xhtml 我有一个名为patients的表,这是页脚:

<f:facet name="footer">
                    <p:outputPanel>
                         <p:growl id="growl" showDetail="true" />
                         <p:commandButton value="Add Patient" icon="ui-icon-extlink"
                        actionListener="#{patientsViewBean.addNewPatientDialog}">
                        <p:ajax event="dialogReturn"
                            listener="#{patientsViewBean.onDialogReturn}" update="patients" />
                    </p:commandButton>
                </p:outputPanel>
</f:facet>

对话框页面是 addNewDialog.xhtml 它包含:

    <!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://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Add New Patient</title>
    <style type="text/css">
.ui-widget {
    font-size: 80%;
}
</style>
</h:head>
<h:body>
    <p:panel style="margin: 2px;">
        <h:form id="form" >
            <p:growl id="msgs" showDetail="true" />
            <p:panel title="Patient Personal Information" header="Patient Personal Information">
                <p:panelGrid style="margin: 2px 2px 2px 2px" columns="2"
                    columnClasses="label, value">
                    <p:outputLabel value="First Name: *"></p:outputLabel>
                    <p:inputText value="#{newPatientView.patient.firstName}"
                        required="true" label="First Name"></p:inputText>
                    <p:outputLabel value="Father Name: *"></p:outputLabel>
                    <p:inputText value="#{newPatientView.patient.fatherName}"
                        required="true" label="Father Name"></p:inputText>
                    <p:outputLabel value="Family Name: *"></p:outputLabel>
                    <p:inputText value="#{newPatientView.patient.familyName}"
                        required="true" label="Family Name"></p:inputText>
                    <p:outputLabel value="Mother Name: *"></p:outputLabel>
                    <p:inputText value="#{newPatientView.patient.motherName}"
                        required="true" label="Mother Name"></p:inputText>
                    <p:outputLabel value="Birth Date"></p:outputLabel>
                    <p:calendar id="bdate" value="#{newPatientView.patient.birthDate}"
                        locale="ar" navigator="true" yearRange="c-120:c" effect="fade"
                        mask="true" pattern="yyyy-MM-dd" required="true" />
                    <p:outputLabel value="Birth Place: "></p:outputLabel>
                    <p:inputText value="#{newPatientView.patient.birthPlace}"
                        required="false" label="Birth Place"></p:inputText>
                </p:panelGrid>
            </p:panel>
            <p:panel title="Contact Information" header="Contact Information">
                <p:remoteCommand name="updateButtonsStatus"
                    process="@this contactInfoTable:buttons"
                    update="contactInfoTable:buttons"></p:remoteCommand>
                <p:dataTable id="contactInfoTable" var="cntctInfo"
                    selectionMode="single" rowKey="#{cntctInfo}"
                    selection="#{newPatientView.cntctInfo}"
                    value="#{newPatientView.patient.contactInfoList}" editable="true"
                    widgetVar="cellCntctInfo">
                    <p:ajax event="rowEditInit"
                        listener="#{newPatientView.onRowEditInit}"
                        oncomplete="updateButtonsStatus()" />
                    <p:ajax event="rowEdit" immediate="true"
                        listener="#{newPatientView.onRowEdit}" update=":form:msgs"
                        oncomplete="updateButtonsStatus()" />
                    <p:ajax event="rowEditCancel"
                        listener="#{newPatientView.onRowCancel}"
                        oncomplete="updateButtonsStatus()" />

                    <p:column headerText="Email">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{cntctInfo.email}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText id="emailInput" value="#{cntctInfo.email}"
                                    style="width:95%" />
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Phone">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{cntctInfo.phone}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText id="phoneInput" value="#{cntctInfo.phone}"
                                    style="width:95%" />
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Type">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{cntctInfo.type}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:selectOneMenu id="typeInput" value="#{cntctInfo.type}"
                                    style="width:100%; height: 95%">
                                    <f:selectItem itemLabel="Main" itemValue="Main"></f:selectItem>
                                    <f:selectItem itemLabel="Home" itemValue="Home"></f:selectItem>
                                    <f:selectItem itemLabel="Work" itemValue="Work"></f:selectItem>
                                    <f:selectItem itemLabel="Perm" itemValue="Perm"></f:selectItem>
                                    <f:selectItem itemLabel="Curr" itemValue="Curr"></f:selectItem>
                                </p:selectOneMenu>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Edit" style="width:32px">
                        <p:rowEditor />
                    </p:column>
                    <f:facet name="footer">
                        <p:outputPanel id="buttons" autoUpdate="true">
                            <p:commandButton value="Add New" process="@this contactInfoTable"
                                actionListener="#{newPatientView.onAddNewContactInfo}"
                                update="contactInfoTable :form:msgs"
                                disabled="#{newPatientView.addContactInfoDisabled}" />
                            <p:commandButton value="Delete Seleced"
                                process="@this contactInfoTable"
                                actionListener="#{newPatientView.onDeleteSelectedContactInfo}"
                                update="contactInfoTable :form:msgs"
                                disabled="#{newPatientView.deleteSelectedContactInfoDisabled}" />
                        </p:outputPanel>
                    </f:facet>
                </p:dataTable>
            </p:panel>
            <p:panel>
                <p:commandButton value="Save" actionListener="#{newPatientView.onDialogApprove}"></p:commandButton>
                <p:commandButton value="Discard" actionListener="#{newPatientView.onDialogCancel}"></p:commandButton>
            </p:panel>
        </h:form>
    </p:panel>
</h:body>
</html>

我有两个烘焙豆 newPatientView ,代码如下:

package me.bttb.crs.beans.patient;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import me.bttb.crs.model.ContactInfo;
import me.bttb.crs.model.Ptnt;

@Named
@ManagedBean
@ViewScoped
public class NewPatientView {
@Inject
private PatientDAO patientDAO;
    private Ptnt patient;
    private List<ContactInfo> cntctInfoList;
    private ContactInfo cntctInfo;
    private boolean addContactInfoDisabled = false;
    private boolean deleteSelectedContactInfoDisabled = true;

    boolean confirm = false;

    public NewPatientView() {
        init();
    }

    public void init() {
        this.setPatient(new Ptnt());
        this.setCntctInfoList(new ArrayList<>());
        this.getPatient().setContactInfoList(this.getCntctInfoList());
    }

    /////////////// EVENTS//////////////////
    public void onAddNewContactInfo(ActionEvent event) {
        if (patient.getContactInfoList() == null)
            patient.setContactInfoList(new ArrayList<>());
        boolean hasNewRow = patient.getContactInfoList().stream()
                .filter(ci -> ci.getEmail() == null && ci.getPhone() == null).count() >= 1;
        if (hasNewRow) {
            FacesMessage msg = new FacesMessage("Can't add new row", "There is an empty row already!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            ContactInfo cntctInfo = new ContactInfo();
            cntctInfo.setType("Main");
            cntctInfo.setPrsn(patient);
            patient.getContactInfoList().add(cntctInfo);
        }
    }

    public void onDeleteSelectedContactInfo(ActionEvent event) {
        if (cntctInfo != null) {
            cntctInfoList.remove(cntctInfo);
            cntctInfo = null;
        }
        this.setDeleteSelectedContactInfoDisabled(true);
        this.setAddContactInfoDisabled(false);
    }

    public void onRowEdit(RowEditEvent event) {
        this.setDeleteSelectedContactInfoDisabled(true);
        this.setAddContactInfoDisabled(false);
        ContactInfo editedContactInfo = (ContactInfo) event.getObject();
        boolean moreThanOne = patient.getContactInfoList().stream().filter(ci -> ci.equals(editedContactInfo))
                .count() > 1;
        if (moreThanOne) {
            patient.getContactInfoList().remove(editedContactInfo);
            FacesMessage msg = new FacesMessage("Row Removed", "The contact information provided is redundent!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void onRowEditInit(RowEditEvent event) {
        if (event.getObject() instanceof ContactInfo) {
            this.setCntctInfo((ContactInfo) event.getObject());
            this.setDeleteSelectedContactInfoDisabled(false);
        }
        this.setAddContactInfoDisabled(true);
    }

    public void onRowCancel(RowEditEvent event) {
        this.setDeleteSelectedContactInfoDisabled(true);
        this.setAddContactInfoDisabled(false);
    }

    public void onDialogCancel() {
        RequestContext.getCurrentInstance().closeDialog(null);
    }

    public void onDialogApprove() {
        RequestContext.getCurrentInstance().closeDialog(patient);
    }


    public PatientDAO getPatientDAO() {
        return patientDAO;
    }

    public void setPatientDAO(PatientDAO patientDAO) {
        this.patientDAO = patientDAO;
    }

    public Ptnt getPatient() {
        return patient;
    }

    public void setPatient(Ptnt patient) {
        this.patient = patient;
    }

    public ContactInfo getCntctInfo() {
        return cntctInfo;
    }

    public void setCntctInfo(ContactInfo cntctInfo) {
        this.cntctInfo = cntctInfo;
    }

    public boolean isAddContactInfoDisabled() {
        return addContactInfoDisabled;
    }

    public void setAddContactInfoDisabled(boolean addContactInfoDisabled) {
        this.addContactInfoDisabled = addContactInfoDisabled;
    }

    public boolean isDeleteSelectedContactInfoDisabled() {
        return deleteSelectedContactInfoDisabled;
    }

    public void setDeleteSelectedContactInfoDisabled(boolean deleteSelectedContactInfoDisabled) {
        this.deleteSelectedContactInfoDisabled = deleteSelectedContactInfoDisabled;
    }

    public List<ContactInfo> getCntctInfoList() {
        return cntctInfoList;
    }

    public void setCntctInfoList(List<ContactInfo> cntctInfoList) {
        this.cntctInfoList = cntctInfoList;
    }

}

以下是我在 patienViewBean 中调用对话框的方法:

package me.bttb.crs.beans.patient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import me.bttb.crs.model.Ptnt;

@Named
@ManagedBean
@ViewScoped
public class PatientsViewBean {
    @Autowired
    private PatientDAO patientDAO;
    private List<Ptnt> patients;
    private List<Ptnt> filterdPatients;

    public PatientsViewBean() {
    }

    public long getPatientsCount() {
        return patientDAO.getPatientsCount();
    }

    public List<Ptnt> refreshAllPatients() {
        this.setPatients(patientDAO.getPatients());
        return this.getPatients();
    }

    public void addNewPatientDialog() {
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("resizable", false);
        options.put("draggable", false);
        options.put("modal", true);
        //options.put("contentHeight", "95%");
        //options.put("height", 400);
        //options.put("width", 700);
        options.put("closable", false);
        RequestContext.getCurrentInstance().openDialog("newPatientDialog", options, null);
    }

    public void onDialogReturn(SelectEvent event) {
        if (event.getObject() != null && event.getObject() instanceof Ptnt) {
            patientDAO.addpatient((Ptnt) event.getObject());
            refreshAllPatients();
            FacesMessage msg = new FacesMessage("Patient Added", "Pateint information added in the database.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            FacesMessage msg = new FacesMessage("operation canceld", "Nothing is saved to the database.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    ////////////////////////////////////

    public PatientDAO getPatientDAO() {
        return patientDAO;
    }

    public void setPatientDAO(PatientDAO patientDAO) {
        this.patientDAO = patientDAO;
    }

    public List<Ptnt> getPatients() {
        return patients;
    }

    public void setPatients(List<Ptnt> patients) {
        this.patients = patients;
    }

    public List<Ptnt> getFilterdPatients() {
        return filterdPatients;
    }

    public void setFilterdPatients(List<Ptnt> filterdPatients) {
        this.filterdPatients = filterdPatients;
    }

}

我做错了什么,每次对话框出现时都不应该重新创建 newPatientView 吗?

请在此解释视图的生命周期。

0 个答案:

没有答案