Welcome!

APIs for the Internet of Things

Max Katz

Subscribe to Max Katz: eMailAlertsEmail Alerts
Get Max Katz via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: XML Magazine

Blog Feed Post

Learning JSF2: Using Flash scope

JSF 2 provides two new scopes on top of the standard Servlet scopes (request, session, application). One of them is the view scope. View scope was covered in Managed beans article. The other scope is Flash which I’m going to cover here. Let’s start with a very simple example.

Managed bean:

@ManagedBean(name = "bean")
public class Bean {
   private String text;
   // getter and setter
 
   public String nextpage (){
       return "page2";
   }
}

As scope is not specified, the default scope is request.

JSF page (page1.xhtml):

<h:form>
   <h:panelGrid>
	<h:outputText value="Text:" />
	<h:inputText value="#{bean.text}" />
	<h:commandButton action="#{bean.nextpage}" value="Click" />
   </h:panelGrid>
</h:form>


JSF page (page2.xhtml):

<h4>#{bean.text}</h4>

This application is very simple. The first page will render this (shown after entering text):

Once button is clicked, we navigate to the next page and text value is displayed:

One thing to note. We didn’t define a navigation rule in JSF configuratin file. We used JSF 2 implicit navigation feature:

public String nextpage (){
       return "page2";
 }

JSF will try to locate page called page2.xhtml and navigate to it. Navigation was was covered in this article.

Another thing to keep in mind that by default JSF does server forward when it invokes navigation. You probably noticed this this causes the URL in browser address bar always be one behind the actual page. This happens because we do a postbback (submit the page to itself) but return a different page to the browser.

One way to solve this problem is to use a redirect. A redirect can be defined in JSF configuration file or also added to our implicit navigation:

public String nextpage (){
       return "page2?faces-redirect=true";
 }

Running the application with above change and navigating to the second page will produce an empty page. When using a redirect, a page is submitted and once we reach the navigation step, the server sends the browser back a response telling it to send a new request to a specified URL. Now, this URL now matches the actual page we would be displaying. While we solved the URL/page matching problem, we now created another problem. As the browser sends a new request, any request-scoped objects will be recreated (or the old one gone). As our bean was in request scope, a new one was created and we lost the original text. That’s where the new Flash scope can help us.

Objects placed inside the Flash scope will be available for the subsequent request and then cleared. In other words, objects placed in Flash scope will survive a redirect. The Flash scope is very similar to temporary Seam conversation.

To place an object into Flash scope we use this:

public String nextpage (){
   ELFlash.getFlash().put("bean",this);   
   return "page2?faces-redirect=true";
 }

Then, to read the values from Flash scope on the result page, we update the page like this:

<h4>#{flash.bean.text}</h4>

this will also work:

<h4>#{flash['bean'].text}</h4>

View scope wouldn’t work here as it works when we are staying on the same view (page). In our case, we navigated to a different view (page).

Read the original blog entry...

More Stories By Max Katz

Max Katz heads Developer Relations for Appery.io, a cloud-based mobile app platform. He loves trying out new and cool REST APIs in mobile apps. Max is the author of two books “Practical RichFaces” (Apress 2008, 2011), DZone MVB (Most Valuable Blogger), and is a frequent speaker at developer conferences. You can find out what Max is up to on his blog: http://maxkatz.org and Twitter: @maxkatz.