【问题标题】:How to reset session scoped bean when re-opening page in new browser tab在新浏览器选项卡中重新打开页面时如何重置会话范围的 bean
【发布时间】:2015-04-04 12:50:24
【问题描述】:

一旦 xhtml 页面 (primefaces) 上的选项卡在提交值后关闭,我重新打开一个新选项卡,该选项卡选择相同的 bean,旧值仍然存在。我想用默认值打开一个新选项卡(支持 bean 的新实例)。

XHTML 页面

<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>


支持 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);

}

【问题讨论】:

  • 然后重置你的backingbean中的值/从你的backingbean
  • 你的backing bean的范围是什么?

标签: jsf-2 primefaces scope managed-bean browser-tab


【解决方案1】:

由于 bean 似乎包含视图范围的数据(只要您在同一浏览器窗口/选项卡中回发,视图范围就存在),您应该将其设为 @ViewScoped,而不是 @SessionScoped。这样,您也不需要手动创建和销毁 bean。只需将 bean 放在正确的范围内即可。

另见:

【讨论】:

  • 我将作用域更改为查看,现在我在 bean 上收到“NotSerializableException”。
  • 如果 bean 或其字段之一不是 Serializable,确实会发生这种情况。异常消息表示需要实现Serializable 的类的完全限定名称。另见 a.o. stackoverflow.com/questions/2294551/…
  • 好的,我知道了。我不得不将范围更改为查看并将我的 web.xml“状态保存方法”从客户端更改为服务器以摆脱“NotSerializableException。BalusC 你对更改视图的响应产生了”NotSerializableException。我发现你回复的一篇帖子解决了这个错误,它纠正了问题,因为你的初始答案对我的问题是正确的,它导致我在 web.xml 中出现配置问题。谢谢。
猜你喜欢
  • 2010-10-05
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2017-08-08
相关资源
最近更新 更多