9

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

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

拳头页.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";
}
...
}

第二页.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

谢谢

4

1 回答 1

8

我刚刚在我的游乐场项目中进行了一些测试,并意识到即使您再次获取页面,使用{flash.keep}. 这就是JSF 文档的解释方式:

实现必须确保即使在 a<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 中测试

于 2014-07-31T11:26:48.853 回答