0

I have two pages(let's say page1 and page2), and one magaedBean for each page (mb1 and mb2). in the mb1, i have an instance of the mb2, with all their attributes previously setted by a method in mb1. Something like this:

public Mb1(){}//Default constructor
private Mb2 mb2;//Instance of Mb2 inside Mb1

what i want is, from a method in mb1, to open the view page2 in a way that the controller of page2(mb2) is the instance stored in the mb1.

Both my managedBeans MUST be ViewScoped, i know i can do it by use session scoped but this is not what i need. Is tere a way to do this?

Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101
  • which version of JSF are you using? I wouldn't make a bean responsible of initializing another one... – Fritz May 06 '13 at 14:14
  • Also, what does Mb1 do that can't Mb2 do by itself? – Fritz May 06 '13 at 14:19
  • it's a communication between the pages... this are two crud pages, each one for a specific entity – Mateus Viccari May 06 '13 at 14:49
  • Why can't you then pass the id (edit) or none at all (create) to the second page instead of a fully created an initialized bean? Also, please, which version of JSF are you using? – Fritz May 06 '13 at 14:53
  • This might be helpful http://stackoverflow.com/questions/4994458/how-can-i-pass-a-parameter-to-a-commandlink-inside-a-datatable/4994833#4994833 – Jitesh May 08 '13 at 19:45

1 Answers1

0

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.

Fritz
  • 9,987
  • 4
  • 30
  • 49