HTML and CSS Reference
In-Depth Information
■
You cannot use the request scope in order to keep the information after the http redirect, because the http
redirect creates a new request, which means that the current request information will be lost.
Note
Listing 2-27 shows the code of the
Survey
managed bean, which includes a single method
save()
that retrieves
the
flash
attributes and then prints them in the console.
Listing 2-27.
The Survey Managed Bean
package com.jsfprohtml5.survey.model;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
@ManagedBean
public class Survey implements Serializable {
public String save() {
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
// Read the information from the flash
String userName = (String) flash.get("userName");
Number age = (Number) flash.get("age");
String sex = (String) flash.get("sex");
Number monthlyIncome = (Number) flash.get("monthlyIncome");
Number travelsAbroad = (Number) flash.get("travelsAbroad");
String travelBy = (String) flash.get("travelBy");
System.out.println("Flash information are: \n{\n" +
"Name: " + userName + ", \n" +
"Age: " + age + ", \n" +
"Sex: " + sex + ", \n" +
"monthlyIncome: " + monthlyIncome + ", \n" +
"travelsAbroad: " + travelsAbroad + ", \n" +
"travelBy: " + travelBy + "\n" +
"}");
// Save the information in the survey database ...
// ...
return "final?faces-redirect=true";
}
}
You can get the
Flash
object using the
getFlash()
API of the
ExternalContext
. After this, you can retrieve the
flash attributes using the
get()
method of the
Flash
object. Finally, you can do whatever you want with the retrieved
flash attributes (such as persisting them in a structured database, or using them for starting a workflow ...).
