jee6 学习笔记 4 - JSF2 Flash Scope

本文深入探讨了JSF2框架中的FlashScope概念及其应用,特别是在实现插入和更新实体后的重定向模式中。通过实例展示了如何在JSF2页面中访问和设置FlashScope变量,并解释了其与常规请求作用域的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSF 2 provides copule of new scopes, among which the "Flash" scope is quite interesting. (i believe the concept is borrowed from other frameworks like the "Stripes" framework)

this is quoted from the Stripes framework documentation of its class net.sourceforge.stripes.controller.FlashScope:
[quote]
A FlashScope is an object that can be used to store objects and make them available as request parameters during this request cycle and the next one. It is extremely useful when implementing the redirect-after-post pattern in which an ActionBean receives a POST, does some processing and then redirects to a JSP to display the outcome. FlashScopes make temporary use of session to store themselves briefly between two requests.

http://stripes.sourceforge.net/docs/current/javadoc/net/sourceforge/stripes/controller/FlashScope.html
[/quote]

The Flash scope could be used for a redirection to show outcomes after a insertion and/or update of entities. Since it's a redirect, instead of the default "forward"; at the server sider, all @RequestScoped variables will be reinitialized and thus would lose values. However, it's often desirable to keep those varaibles available to the "next" page. The new Flash scope fills this gap.


API example:


ExternalContext cntxt = FacesContext.getCurrentInstance().getExternalContext();

// lets set logged in user into to "flash" scope.
// 1. it can be accessed like this: "#{flash.USER-key.username}"; in the JSF2 pages.
// 2. or in the backing bean code: User user = (User) flash.get("USER-key");
Flash flash = cntxt.getFlash();
flash.put("USER-key", this.user);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值