在JSF2中重新初始化会话bean(sessionScopped中的bean)

时间:2013-12-16 20:21:40

标签: jsf-2

我使用的是JSF 2.0,我有一个bean opcD(@SessionScoped)。因为我在提交表单并返回到页面之后在SessionScoped中定义了bean,所以数据保留在逻辑字段中!但是如果我再次回到它里面,我想重新初始化我的bean。

但我不想破坏会议!

@ManagedBean(name="opcD") 
@SessionScoped
public class NewOPCDetailBean implements Serializable {

请在下面找到我鳕鱼的一部分。我从与opc managedBean相关的页面(newopcheadpage.xhtml)转到与managedBean opcD相关的页面(newopcdetailpage.xhtml)。提交页面(newopcdetailpage.xhtml)后,如果我想返回页面(newopcheadpage.xhtml),我希望看到这些字段为空白(bean重新初始化)。

@ManagedBean(name =“opc”,eager = true)@SessionScoped 公共类NewOPCHeadBean实现Serializable {

private long partID;
private String partDesc;
private String taskDesc;
private Date date;
private String opcDesc;
private List<Part> partList;

public long getPartID() {
    return partID;
}

public void setPartID(long partID) {
    this.partID = partID;
}

public String getPartDesc() {
    return partDesc;
}

public void setPartDesc(String partDesc) {
    this.partDesc = partDesc;
}

public String getTaskDesc() {
    return taskDesc;
}

public void setTaskDesc(String taskDesc) {
    this.taskDesc = taskDesc;
}

public Date getDate() {
    return new Date();
}

public void setDate(Date date) {
    this.date = new Date();
}

public String getOpcDesc() {
    return opcDesc;
}

public void setOpcDesc(String opcDesc) {
    this.opcDesc = opcDesc;
}

public List<Part> getPartList() {
    return partList;
}

public void setPartList(List<Part> partList) {
    this.partList = partList;
}

public List<Part> getPartsList(ActionEvent e)
{

    HibernateDA hDA = new HibernateDA();
    partList  = hDA.partsList();

    return partList;
}
public void showPartDesc()
{
    HibernateDA hDA = new HibernateDA();
    partDesc  = hDA.showPartDescription(partID);
    setPartDesc(partDesc);
}

public String submit()
{

    return "/pages/admin/newopcdetailpage";
}

public void resetBean()
{
    opcDesc="";
    partID=0;
    partDesc="";
}

}

@ManagedBean(name =“opcD”)@ ViewScoped 公共类NewOPCDetailBean实现Serializable {

private long partID;
private String taskDesc;
private long partNumber;
private Part part;
private Date date;
private String opcDesc;
private long task_opc_number;
private long task_nominal_time;
private OPCDetail opcDetail;
private List<Part> partList;

private static final ArrayList<OPCDetail> opcDetailList = new ArrayList<OPCDetail>();
//private static ArrayList<OPCDetail> opcDetailList = new ArrayList<OPCDetail>();

public ArrayList<OPCDetail> getOpcDetailList() {
    return opcDetailList;
}


public long getPartID() {
    return partID;
}

public void setPartID(long partID) {
    this.partID = partID;
}

public long getPartNumber() {
    return partNumber;
}

public void setPartNumber(long partNumber) {
    this.partNumber = partNumber;
}

public Part getPart() {
    return part;
}

public void setPart(Part part) {
    this.part = part;
}

public String getTaskDesc() {
    return taskDesc;
}

public void setTaskDesc(String taskDesc) {
    this.taskDesc = taskDesc;
}

public long getTask_nominal_time() {
    return task_nominal_time;
}

public long getTask_opc_number() {
    return task_opc_number;
}

public void setTask_opc_number(long task_opc_number) {
    this.task_opc_number = task_opc_number;
}

public void setTask_nominal_time(long task_nominal_time) {
    this.task_nominal_time = task_nominal_time;
}

public OPCDetail getOpcDetail() {
    return opcDetail;
}

public void setOpcDetail(OPCDetail opcDetail) {
    this.opcDetail = opcDetail;
}

public List<Part> getPartList() {
    return partList;
}

public void setPartList(List<Part> partList) {
    this.partList = partList;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getOpcDesc() {
    return opcDesc;
}

public void setOpcDesc(String opcDesc) {
    this.opcDesc = opcDesc;
}

public List<Part> getPartsList(ActionEvent e)
{

    HibernateDA hDA = new HibernateDA();
    partList  = hDA.partsList();

    return partList;
}

public void takePartNumber(ActionEvent e) throws Exception
{
    Map m = e.getComponent().getAttributes();
    partID = (Long)m.get("pID");
    HibernateDA hDA = new HibernateDA();
    part = hDA.takePart(partID);
    partNumber = part.getPart_number();
    opcDesc = (String) m.get("opcDesc");
    date = (Date) m.get("opcDate");

}

public void addAction()
{

    OPCDetail opcDetail1 = new OPCDetail(this.task_nominal_time,this.taskDesc);
    int i = 0;
    int j=0;
    if (opcDetailList != null){
        while (i < opcDetailList.size())
        {
            String tDesk = this.taskDesc.toLowerCase();
            if (opcDetailList.get(i).getTask_desc().equals(tDesk))
            {
                j = 1;
                break;
            }
            i++;
        }
        if(j == 0)
        {
            opcDetailList.add(opcDetail1);
        }
    }
    taskDesc = "";
    task_nominal_time = 0;
}

public void onEdit(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Item Edited",((OPCDetail) event.getObject()).getTask_desc());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Item Cancelled");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    opcDetailList.remove((OPCDetail) event.getObject());
}

public void resetBean()
{
   taskDesc = "";
   task_nominal_time=0;
}

public String submitNewOPC()
{
    int i = 0;
    List<OPCDetail> opcDetailList1 = new ArrayList<OPCDetail>();
    if (opcDetailList != null && opcDetailList.size() != 0){
        while (i < opcDetailList.size())
        {
            OPCDetail opcDetail1 = new OPCDetail(i+1,opcDetailList.get(i).getTask_nominal_time(),opcDetailList.get(i).getTask_desc());
            opcDetailList1.add(opcDetail1);
            i++;
        }

        HibernateDA hDA = new HibernateDA();
        OPC opc1 = new OPC();
        opc1.setOpc_date(date);
        opc1.setOpc_desc(opcDesc);
        opc1.setPart(part);

        opc1.setListOfTasks(opcDetailList1);

        hDA.addNewOPC(opc1);
    }

    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("#opcD}");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("#opc}");
    return "/index?faces-redirect=true";
}

}

    

    <div style="border-bottom:2px solid #FFF">
        <ui:insert name="header" >
            <ui:include src="/header.xhtml" />
        </ui:insert>
    </div>
    <div style="margin:0px 380px 0px 0px">
        <h:form>

            <p:panel header="Add New OPC" style="font-size: .6em">
                <p:messages autoUpdate="true"/>

                <h:panelGrid id="grid" columns="2" cellpadding="5">
                    <h:outputLabel for="opcDate" value="Date (dd-MM-yyyy):" style="font-weight:bold; font-size: .8em"/>
                    <p:inputText id="opcDate" value="#{opc.date}" required="true" label="Date(dd-MM-yyyy):" disabled="true">
                        <f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
                    </p:inputText>
                    <p:message for="opcDate" />
                    <h:outputText value="" />

                    <h:outputLabel for="opcDesc" value="OPC Description:" style="font-weight:bold; font-size: .8em"/>
                    <p:inputText id="opcDesc" value="#{opc.opcDesc}" required="true" label="OPC Description"/>
                    <p:message for="opcDesc" />
                    <h:outputText value="" />

                    <h:outputText value="Part Number " style="font-weight:bold; font-size: .8em" />
                    <p:selectOneMenu value="#{opc.partID}" required="true">
                        <p:ajax listener="#{opc.showPartDesc()}" update="partdesc" />
                        <f:selectItem itemLabel="Select One" itemValue="" />
                        <f:selectItems value="#{opc.partList}" var="parts" itemLabel="#{parts.part_number}" itemValue="#{parts.id}"/>
                    </p:selectOneMenu>

                    <h:outputLabel for="partdesc" value="Part Description:" style="font-weight:bold; font-size: .8em"/>
                    <h:outputText id="partdesc" value="#{opc.partDesc}" label="Part  Description:"/>
                    <p:message for="partdesc" />
                    <h:outputText value="" />

                </h:panelGrid>
            </p:panel>

            <p:commandButton value="Register" action="#{opc.submit()}" actionListener="#{opcD.takePartNumber}" ajax="false"  icon="ui-icon-check" validateClient="true" style="margin-right:10px">
                <f:attribute name="pID" value="#{opc.partID}"/>
                <f:attribute name="opcDate" value="#{opc.date}"/>
                <f:attribute name="opcDesc" value="#{opc.opcDesc}"/>
            </p:commandButton>
        </h:form>
    </div>
    <div style="padding-left: 500px">
        <h:form style="font-size: .8em">
            <p:commandButton value="Functions" action="/index?faces-redirect=true" ajax="false" icon="ui-icon-wrench"/>
        </h:form>
    </div>
</div>

    

    <div style="border-bottom:2px solid #FFF">
        <ui:insert name="header" >
            <ui:include src="/header.xhtml" />
        </ui:insert>
    </div>
    <div style="margin:0px 380px 0px 0px">
        <h:form>

            <p:panel header="Add New OPC" style="font-size: .6em">
                <p:messages autoUpdate="true"/>

                <h:panelGrid id="grid" columns="4" cellpadding="5">
                    <p:row>
                        <p:column colspan="10">
                            <h:outputLabel for="opcDate" value="Date (dd-MM-yyyy):" style="font-weight:bold; font-size: .8em"/>
                            <p:inputText id="opcDate" value="#{opc.date}" required="true" label="Date(dd-MM-yyyy):" disabled="true">
                                <f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
                            </p:inputText>
                            <p:message for="opcDate" />
                            <h:outputText value="" />
                            <h:outputLabel for="opcDesc" value="OPC Description  :" style="font-weight:bold; font-size: .8em"/>
                            <p:inputText id="opcDesc" value="#{opc.opcDesc}" required="true" label="OPC Description" disabled="true"/>
                            <p:message for="opcDesc" />
                            <h:outputText value="" />
                        </p:column>
                    </p:row>
                    <p:row>
                        <p:column>
                            <h:outputLabel for="partnum" value="Part Number:" style="font-weight:bold; font-size: .8em"/>
                            <p:inputText id="partnum" value="#{opcD.partNumber}" required="true" label="Part Number" disabled="true"/>
                        </p:column>
                        <p:column>
                            <h:outputLabel for="partdesc" value="Part Description:" style="font-weight:bold; font-size: .8em"/>
                            <p:inputText id="partdesc" value="#{opc.partDesc}" required="true" label="Part Description" disabled="true"/>
                        </p:column>
                    </p:row>
                </h:panelGrid>
            </p:panel>
        </h:form>
    </div>
    <div style="padding-left: 500px">
        <h:form style="font-size: .8em">
            <p:commandButton value="Functions" action="/index?faces-redirect=true"   ajax="false" icon="ui-icon-wrench"/>
            <p:commandButton value="Submit" action="#{opcD.submitNewOPC}"   ajax="false" icon="ui-icon-wrench">
                <f:actionListener binding="#{opc.resetBean()}"/>
            </p:commandButton>
        </h:form>
    </div>
</div>

<div>
    <h:form id="form1">
        <p:growl id="messages" showDetail="true"/>
        <p:panel header="OPC Detail" style="width: 400px;font-size: .6em">
            <p:panelGrid columns="2">
                <h:outputLabel for="taskDesk" value="Task Description: " />
                <p:inputText id="taskDesk"  value="#{opcD.taskDesc}"/>
                <h:outputLabel for="tasknumTime" value="Task Numinal Time: " />
                <p:inputText id="tasknumTime" value="#{opcD.task_nominal_time}"/>
                <f:facet name="footer">
                    <h:commandButton value="Register Item" action="#{opcD.addAction()}"/>
                </f:facet>
            </p:panelGrid>
            <p:spacer height="30px;"/>
            <p:dataTable value="#{opcD.opcDetailList}" var="o"  widgetVar="50" style="font-size: .3em" editable="true">
                <f:facet name="header">
                    OPC Detail List
                </f:facet>
                <p:ajax event="rowEdit" listener="#{opcD.onEdit}" update=":form1:messages" />
                <p:ajax event="rowEditCancel" listener="#{opcD.onCancel}" update=":form1:messages" />
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Task Desc" />
                    </f:facet>
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{o.task_desc}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{o.task_desc}" style="width:100%"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Numinal Time" />
                    </f:facet>
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{o.task_nominal_time}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{o.task_nominal_time}" style="width:100%"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="" style="width:50px">
                    <p:rowEditor />
                </p:column>
            </p:dataTable>
        </p:panel>
    </h:form>
</div>

2 个答案:

答案 0 :(得分:0)

我不完全明白你想要实现的目标......

  

但是如果我再次回到它里面,我想重新初始化我的bean。

你的意思是,当你离开由bean驱动的页面时,当你导航回到它时,再次重新初始化bean?

这就是@ javax.faces.ViewScoped bean的用途。只要您在视图中,它就会将字段数据保存在内存中。当您远离它时,数据不再可用。所以把你的bean变成@ViewScoped。

答案 1 :(得分:0)

看来你正在以错误的方式构建你的bean。对于您的情况,您应该使用@RequestScoped为表单创建另一个辅助bean。然后,您可以将@SessionScoped注入@RequestScoped bean,以便将您需要的任何内容存储到会话中。