jsf-2 - 如何在页面重新加载时保持JSF Flash作用域参数?

标签 jsf-2 flash-scope

我使用Flash作用域在@viewscoped Controller 之间传递设置对象。但是,如果我在其中一个页面上重新加载页面,则Flash映射为空并且设置对象未初始化。是否可以在页面重新加载时保持Flash作用域?

我的存储/检索设置的源代码:

FistPage.xhtml

...
<p:commandButton value="next"
    action="#{firstPageController.transferConfig}"  
    process="@this" />
...

FirstPageController.java
@ManagedBean(name = "firstPageController")
@ViewScoped
public class FirstPageController {
...
public String transferConfig() {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("searchConfig",   searchConfig);
return "/secondPage.xhtml?faces-redirect=true";
}
...
}

SecondPage.xhtml
...
<h:outputLabel value="value">
    <f:event type="preRenderComponent" listener="#{secondPageController.onPageLoad()}"/>
</h:outputLabel>
...

SecondPageController.java
@ManagedBean(name = "secondPageController")
@ViewScoped
public class SecondPageController {
    ...
    public void onPageLoad() 
    {
        flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

        searchConfig = ((SearchFilterConfig) flash.get("searchConfig"));

        flash.putNow("searchConfig", searchConfig);

        flash.keep("searchConfig");
    }
    ...
}

我使用Mojarra 2.1.29

谢谢

最佳答案

我刚刚在我的运动场项目中进行了一些测试,并且意识到即使使用{flash.keep}再次获取页面,实际上也可以保持Flash参数的状态。这就是JSF docs的解释方式:

The implementation must ensure the proper behaviour of the flash is preserved even in the case of a <navigation-case> that contains a <redirect />. The implementation must ensure the proper behavior of the flash is preserved even in the case of adjacent GET requests on the same session. This allows Faces applications to fully utilize the Post/Redirect/Get design pattern.



在这里,您有一个不错的基本测试用例:

page1.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head />
<h:body>
    <h:form>
        <h:button id="nextButton" value="Next (button)" outcome="page2.xhtml" />
        <c:set target="#{flash}" property="foo" value="bar" />
    </h:form>
</h:body>
</html>

page2.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<head />
<body>foo = #{flash.keep.foo}
</body>
</html>

只需打开第一页并单击按钮,即可将您重定向到第二页。然后根据需要多次刷新第二页,您会发现该参数持续存在。

在Mojarra 2.2.6中测试

关于jsf-2 - 如何在页面重新加载时保持JSF Flash作用域参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25011836/

相关文章:

jsf - JSF 应用程序的审计跟踪 - 全局 valueChangeListener 可能吗?

java - Spring 闪光范围

jsf-2 - 重定向后 Flash 范围内的对象不可用

java - jsf2 flash 的生命周期比它应该的要长吗?

firefox - Mojarra JSF 中关于 flash 的异常

jsf - 从 Tomcat 中的不可用资源重定向

jsf - 基于bean属性动态渲染组件

jsf-2 - post 在 JSF2 flash 作用域中设置一个值后,它在对页面的第二次 GET 请求中再次可见(Flash 作用域被认为是有害的)

java - 在对象中保存 session 属性(Java)

jsf - 如何获取最初请求的页面的url?