In JSF 2.0, amongst Request, Session etc, there is the new scope called Flash. (However there is no annotation @FlashScope, and you can't put bean in this scope). It's concept is taken from Ruby on Rails. Data in Flash scope are available for current and for next request, but not for subsequent requests. Usage is very simple.
On first page (e.g. index.html):
However, there is a way to make data in Flash scope alive a bit longer. It is enough if in page.xhtml we change #{flash.text} to #{flash.keep.text}. This way data is not removed from the scope after first request, but is available for one more.
In managed beans, you can get flash scope by
On first page (e.g. index.html):
<h:form> <h:inputtext value="#{flash.text}"> <h:commandbutton action="page?faces-redirect=true" value="To page"> </h:form>And this is the page we are redirecting to, page.xhtml:
<h:form> <h:outputtext value="#{flash.text}"> </h:form>Why would you need such scope? Imagine you want to enter data on one page, then redirect to another page, and display this data. If not for redirect, we could use Request scope. But with redirect value is sent with request, then browser is redirected to another page, and makes another request. Request scoped value is gone. In such case Flash scope becomes useful. We can put value in this scope with one request, and when another request is made, the value is still there. But it is not stored in session, and is not available for subsequent requests.
However, there is a way to make data in Flash scope alive a bit longer. It is enough if in page.xhtml we change #{flash.text} to #{flash.keep.text}. This way data is not removed from the scope after first request, but is available for one more.
In managed beans, you can get flash scope by
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
2 comments:
Hello, Paweł
I would like to ask you one question - what are your suggestions while using Flash Scope, but not only to use value on next page, but to keep values in the same controller without using session scoped beans. Let's say I have an object and I don't want to put it into session scoped bean, I want to use it in Request Scoped controller and have it's value constanly available. Should I put it into Flash scope or view map before redirect and the get it from empty field getter? What's the best way to get it back if I don't use it directly on the view?:)
Post a Comment