jsf-2 - 在新浏览器选项卡中重新打开页面时如何重置 session 作用域 bean

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

提交值后关闭 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)字符串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);

}

最佳答案

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

另请参阅:

关于jsf-2 - 在新浏览器选项卡中重新打开页面时如何重置 session 作用域 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28330866/

相关文章:

jsf-2 - JSF2.0 相当于 HTML anchor 标记的是什么

jsf-2 - <p :growl> and <p:messages> in a same page

javascript - 将变量添加到 jQuery $ 范围是明智的还是会污染它?

html - 未明确关闭的组件呈现的元素 td

jsf - PrimeFaces TypeError : PF(. ..) 未定义

javascript - 从 Angular 范围内的数组获取所有对象

javascript - 传递给 __defineGetter__ 的函数中 "this"的值是多少

jsf - Primefaces FileUpload 事件未触发 - JSF 2.0

validation - JSF 2.0;验证器标签 "disabled"取决于页面中的值

java - 将请求参数从托管 Bean 传递到 jsf 页面