gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:08:00 24 4
gpt4 key购买 nike

我使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com