`

jee6 学习笔记 4 - JSF2 Flash Scope

    博客分类:
  • JEE
阅读更多
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:
引用

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


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);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics