了解JSF2中的Flash作用域 [英] Understand Flash Scope in JSF2

查看:127
本文介绍了了解JSF2中的Flash作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,在面孔请求生命周期中放置在Flash作用域内的对象将可用于下一个面孔请求生命周期,然后清除.

From what I understand , objects placed inside the Flash scope in a faces request lifecycle will be available for the next faces request lifecycle and then clear.

假设我有两个页面:

page01.xhtml :

<h:form>
    <h:commandButton  action="#{page01Bean.action}" />
</h:form>

Page01Bean:

@ManagedBean
@RequestScoped
public class Page01Bean {

        public void action(){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("fooKey", "fooValue");
        }

}

page02.xhtml :

<h:outputText value="#{flash.fooKey}"/> 

因此,当单击page01.xhtml中的按钮时,将启动人脸请求生命周期(例如生命周期A),并在名为fooKey

So when the button in page01.xhtml is clicked , a faces request life-cycle (say life-cycle A) starts and set the value to the flash under the key called fooKey

然后我打开另一个浏览器选项卡并浏览page02.xhtml.另一个面孔请求生命周期(例如生命周期B)开始呈现此页面.我希望生命周期B可以访问其先前生命周期的闪存作用域(即生命周期A)并在page02.xhtml中显示fooValue.但是,它什么也不显示.

Then I open another browser tab and browse page02.xhtml . Another faces request lifecycle (say lifecycle B) starts to render this page . I expected that lifecycle B can access its previous lifecycle 's flash scope (i.e life-cycle A) and display fooValuein page02.xhtml. However , it displays nothing.

请更正我对本示例中关于Flash作用域的误解.非常感谢

Please correct me what I misunderstand about the flash scope in this exmaple. Thanks so much

推荐答案

简而言之,存储在Flash作用域中的变量将在重定向后保留下来,之后将被丢弃.在实现Post-Redirect-Get模式时,这真的很有用.

In short, variables stored in the flash scope will survive a redirection and they will be discarded afterwards. This is really useful when implementing a Post-Redirect-Get pattern.

如果您尝试通过重定向导航到另一个页面并访问负载属性,则它们将在那里.完成该请求后,闪存中的值将被丢弃.例如:

If you try to navigate to another page by redirect and access the attributes on load, they will be there. After that request is done the values in the flash will be discarded. For example:

您位于page1.xhtml中,并且具有一个CommandLink,该链接可使用这种方法重定向到新页面(注意:我将使用隐式导航).

You're in page1.xhtml and you have a commandLink that redirects to a new page with a method like this one (Note: I'll use implicit navigation).

public String navigateToPageB() {
    FacesContext.getCurrentInstance().getExternalContext().getFlash().put("param1", "Hello World!");
    return "pageB?faces-redirect=true";
}

呈现pageB.xhtml时,您可以通过EL表达式(例如,

When pageB.xhtml is rendered, you can access those values by EL expressions such as

<h:outputLabel value="#{flash['param1']}" />

将显示"Hello World!"我们之前保存在navigationToPageB中的字符串.

which will display the "Hello World!" string we saved earlier in navigateToPageB.

关于您的问题,通过在资源管理器中打开一个新标签页,您将无法访问与在先前标签页上访问的上下文相同的内容,因此您的变量将在那里不可用.

As for your question, by opening a new tab in your explorer you're not accessing the same context you were accessing on your previous tab, so your variable will not be available there.

这篇关于了解JSF2中的Flash作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆