Flash范围如何帮助实现JSF2.0中的PostRedirectGet(PRG)模式 [英] How can Flash scope help in implementing the PostRedirectGet (PRG) pattern in JSF2.0

查看:118
本文介绍了Flash范围如何帮助实现JSF2.0中的PostRedirectGet(PRG)模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关JSF中PRG模式的Balusc博客

a>提及的地方:


本文针对JSF 1.2。对于JSF 2.0,使用新的Flash范围可以更容易实现


我想知道如何 flash scope帮助我们实现这一目标吗?

解决方案

调用 Flash#setKeepMessages( ) true 在呈现响应阶段之前指示JSF将面孔消息存储在闪存范围中并添加 faces-redirect = true 查询字符串参数以执行重定向。

  public String submit(){
// ...

FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,Success!,null));
context.getExternalContext()。getFlash()。setKeepMessages(true);
returnnextpage?faces-redirect-true;
}

这样就不需要收集来自面向上下文,并在重定向之前将它们存储在会话中,并在第一个下一个请求中将它们从会话中删除,并在重定向后将它们重新添加到面部上下文中。



工作方式大致相同。这些消息通过一个唯一的标识符存储在会话中,该标识符在响应中被作为一个cookie传递,并且这些消息(和cookie)已经从已经通过cookie返回的第一个下一个请求中从会话中移除(这是毕竟,这是一个更强大的实现,尽管终端用户在同一时刻在 发送2个HTTP请求的机会非常小 - 或者它必须是机器人)。 p>

I was reading Balusc blog on PRG pattern in JSF where it is mentioned that :

This article is targeted on JSF 1.2. For JSF 2.0, this can easier be achieved using the new Flash scope.

I wanted to find out how can flash scope help us in achieving the same ?

解决方案

Call Flash#setKeepMessages() with true before render response phase to instruct JSF to store the faces messages in the flash scope and add faces-redirect=true query string parameter to the outcome to perform a redirect.

public String submit() {
    // ...

    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", null));
    context.getExternalContext().getFlash().setKeepMessages(true);
    return "nextpage?faces-redirect-true";
}

This way there's no need for a phase listener which collects the faces messages from the faces context and stores them in the session before redirect and removes them from the session on the firstnext request and re-adds them to the faces context after redirect.

The flash scope works roughly the same way. The messages are stored in the session by an unique identifier which is in turn been passed as a cookie in the response and those messages (and the cookie) are been removed from the session on the firstnext request which has passed the cookie back (which is, after all, a more robust implementation although the chance is very little that an enduser will send 2 HTTP requests on the same session at exactly the same moment — or it must be a robot).

这篇关于Flash范围如何帮助实现JSF2.0中的PostRedirectGet(PRG)模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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