RoR Flash Scope in JSF-2

21 Oct

While browsing through the Mojarra Sourcecode, I’ve found a interesting set of classes, all bundled around the keyword “Flash”:


When looking through the files, I found out, that the famous Flash-Scope of Ruby on Rails (my friend Peter told me about that scope in RoR about a year ago when we where doing stuff with JSF 1.2) got ported to JSF-2.

So, what you now basically can do, is to put an object into flash-scope in one request, e.g.:

<h:form id="nextForm" prependId="false">
<c:set target="#{flash}" property="foo" value="bar" />

(Taken from ./jsf-ri/systest/web/flash01.xhtml)

and retrieve it in the next request (but not the request after that one) simply by accessing it through EL

foo = ${}
(Taken from ./jsf-ri/systest/web/flash02.xhtml)

Also you can put objects into the flash scope in your controllers:


I know that looks quite dirty because one is polluting the controller with a Singleton, but when done right, you can still make your code perfectly testable 😉

The files you need to take a look at are:

  • The Interface: ./jsf-api/src/main/java/javax/faces/context/
  • The Implementation: ./jsf-ri/src/com/sun/faces/context/flash/
  • The Test-Case: ./jsf-ri/systest-per-webapp/flash/src/java/com/sun/faces/systest/ the test code use some cleanup!)

Update: I found a blog post from Ed Burns about using the Flash Scope, although its still JSP based, it should also give a good introduction!

Comment Form