2

Once the tab on the xhtml page (primefaces) is closed after submitted the values, I re-open a new tab which selects the same bean the old values are still there. I'd like to open a new tab with the default values (new instance of the backing bean).

XHTML page

<h:form>
    <p:layout style="min-width:400px;min-height:700px" >
        <p:layoutUnit position="west" style="min-width:200px;min-height:50px">

            <ui:include src="Menu.xhtml" />


        </p:layoutUnit>

        <p:layoutUnit  position="center" >
            <p style="text-align:center">Welcome</p>

            <p:tabView dynamic="false" widgetVar="tabView">
                <p:ajax event="tabClose" listener="#{requestCalculation.onTabClosed}"/>
                <c:forEach items="#{requestCalculation.formTabs}" var="listItem">
                    <p:tab title="#{listItem.formTitle}"  closable="true" >
                        <ui:include src="#{listItem.formPage}" />
                     </p:tab>
                </c:forEach>

            </p:tabView>
                <h:panelGrid columns="2">
                    <h:commandButton value="Calculate" action = "#{requestCalculation.calculate}"/>
                    <h:commandButton value="Reset" action = "#{requestCalculation.resetPage}"/>
                </h:panelGrid>
        </p:layoutUnit>

    </p:layout>

</h:form>


Backing Bean

public String loadForm(TcsBase loadForm){

    id = loadForm.getId();

    ViewTabs tab = new ViewTabs();
    tab.setFormTitle("Form".concat(id));
    tab.setFormPage("Form".concat(id).concat(".xhtml"));
    formTabs.add(tab);  

    calcReq.getFormCollection().add(loadForm);

    System.out.println(loadForm.getId());
    return "Main";
}

public void onTabClosed(TabCloseEvent e){
    TabView tabView = (TabView) e.getComponent();
   int closingTabIndex = tabView.getChildren().indexOf(e.getTab());
   removeForm(closingTabIndex);

}

public void removeForm(int index){
    calcReq.getFormCollection().remove(index);
    formTabs.remove(index);

}

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Rod
  • 43
  • 2
  • 4

1 Answers1

2

As the bean appears to hold view scoped data (the view scope lives as long as you're postbacking in the same browser window/tab), you should make it @ViewScoped, not @SessionScoped. This way you also don't need to mess around with manually creating and destroying beans. Just put the bean in the right scope in first place.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I changed the scoped to view and now I'm receiving a "NotSerializableException" on the bean. – Rod Feb 05 '15 at 12:49
  • That can indeed happen if the bean or one of its fields is not `Serializable`. The exception message represents the fully qualified name of the class which needs to implement `Serializable`. See also a.o. http://stackoverflow.com/questions/2294551/java-io-writeabortedexception-writing-aborted-java-io-notserializableexception/11692208#11692208 – BalusC Feb 05 '15 at 13:08
  • Ok, I got it. I had to change the scope to View and change my web.xml "State Saving Method" from client to Server to get rid of the "NotSerializableException. BalusC your response to change the view produced the "NotSerializableException. I found a post that you responded to that addressed this error which corrected the problem, there for your initial answer was correct for my problem and it led me to a configuration issue in my web.xml. thanx. – Rod Feb 05 '15 at 13:40