Why is a managed bean responsible of initializing another one? I'd rather provide the necessary information to Mb2
so he can initialize himself.
If you're set on passing an object and avoiding Mb2
's self initialization (and assuming you're using JSF 2), I suggest you take a look at JSF's Flash Scope.
Tough Flash is not a scope per se, it's a feature based (AFAIK) on Ruby's implementation of the POST - REDIRECT - GET pattern. Instead of having a bean as a parameter, you can create your own object that stores the data and have Mb2
operate on it.
When you need to navigate to page2
, just store that item in the flash and retrieve it later, when Mb2 is created.
To store it you need the Flash
, which can be obtained in multiple ways:
FacesContext.getCurrentInstance().getExternalContext().getFlash();
Or
@ManagedProperty("#{flash}")
private Flash flash;
public void setFlash(Flash jsfFlash) {
flash = jsfFlash;
}
Flash itself is a map (just like the ones for a request and the session), so you can put your object there:
ManagingObject o = new ManagingObject();
//Initialize the object and set the desired properties.
flash.put("ManagingObject", mo);
You can, then, get that object in Mb2
by obtaining the Flash
and then taking the object from it, for example, in a preRenderView
defined event.