如何在页面重新加载时保持JSF flash范围参数?

时间:2014-07-29 09:14:45

标签: jsf-2 flash-scope

我使用flash scope在@viewscoped控制器之间传递一个设置对象。但是如果我在其中一个页面上重新加载页面,则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

由于

1 个答案:

答案 0 :(得分:6)

我刚刚在我的游乐场项目中做了一些测试,并意识到即使你再次 GET 页面,保持flash参数的状态实际上也是可能的,使用{flash.keep}。这就是JSF docs解释它的方式:

  

即使在<navigation-case>包含<redirect />的情况下,实现也必须确保保留闪存的正确行为。即使在同一会话中发生相邻的 GET 请求,实现也必须确保保留闪存的正确行为。这允许Faces应用程序充分利用 Post / Redirect / Get 设计模式。

在这里,你有一个很好的基本测试用例:

<强> 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中测试