Domanda

Our problem is a very basic, simple implementation of editing the database using JSF + EJB.

Keeping things short: two separate XHTML views, use two separate Managed Beans @RequestScope.

WebuserListBean and EditWebuserBean, and with @ManagedProperty we inject WebuserListBean, so we could obtain selected user data. So far no problems. Views are filled with data succesfully!

BUT! We want to be able to edit the user! And here (to my surprise) we cannot overcome the problem.

1st try: Because the request scoped bean is dead after filling the view, on Save() method the @PostConstruct tries to launch again, of course it can't. So we couldn't even obtain it from database or anything.

2nd try: Because the request scoped bean is dead after filling the view, if we do not set up user as field in @postconstruct, we lose our connection with user object which was linked on previous view (and injected, but now that view is dead too).

3rd try: Cannot inject RequestScope in ViewScoped

Ok and our restrictions, because we think it's wrong way:

  1. We dont want to create a SessionScoped Managed Bean for this
  2. We dont want to use any params etc. We want to use EJB
  3. We are not sure if we could store data in Stateful session bean which is our endpoint for the module? Is it proper approach?

Thanks for any advice, we could paste some code but i guess it is pointless! Cheers!

È stato utile?

Soluzione

There are many ways to do it, but I recommend using the flash if the pages involved in the navigation are in the same folder (I recently found out reading a BalusC answer that there is a known issue with the flash, in which it will not hold values when navigating between pages in different folders!).

The flash is a way to hold parameters for a little longer than the context of a single request (concept taken from Ruby if I'm not mistaken, someone correct me if I'm wrong), allowing for the sent parameters to be fetched in a subsequent view, for example. Those values are discarded in the second request issued after saving them, if I'm not mistaken. You can inject the flash in your managed beans like this:

@ManagedProperty("#{flash}")
private Flash flash;

public void setFlash(Flash newFlash) {
    flash = newFlash;
}

Then, you access it like a map with the put and get methods. If you use the put method in a bean, return a redirection rule and, on the second bean, use the get method your object should be there.

You can also find a highly comprehensible guide of communication in JSF (listing a really extensive list of options) here, in particular if you need to navigate between pages in different folders.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top