0
votes

I am using JSF 2.0 , I have a bean opcD (@SessionScoped). since I have defined the bean in SessionScoped after submitting the form and returning to the page the data remains inside the fields that is logic! But I would like to reinitialize my bean if I return again inside it.

But I do not want to destroy the session!

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

Please find in below a part of my cod. I go from page (newopcheadpage.xhtml) relevant to opc managedBean to page (newopcdetailpage.xhtml)relevant to managedBean opcD. After submiting page(newopcdetailpage.xhtml) if I want to return to page (newopcheadpage.xhtml), I would like to see the fields blank(beans become reinitialized ).

@ManagedBean(name="opc",eager = true) @SessionScoped public class NewOPCHeadBean implements 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 public class NewOPCDetailBean implements 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

2 Answers

0
votes

I don't fully understand what are you trying to achieve ....

But I would like to reinitialize my bean if I return again inside it.

You mean, when you navigate away from page that is driven by the bean, and than when you navigate back to it, to reinitialize the bean again?

That is what @javax.faces.ViewScoped bean is for. It keeps the data of the fields in memory as long as you are on the view. When you navigate away from it, the data is no longer available. So turn you bean into @ViewScoped.

0
votes

It seems you're structuring your beans in the wrong way. For your case, you should create another backing bean for the form using @RequestScoped. Then you can inject your @SessionScoped into the @RequestScoped bean to store whatever you need into the session.