0

I have a problem with opening a sessionScoped managed bean from another. I have a page that has a table wich contains list of objects. On row click I am navigating to another page and displaying its contents. I am sending the clicked row in sessionMap object or by Flash. The object is sent and read in the other page and data are displayed. In the other page I am receiving the sessionMap or Flash in the @PostConstruct method.

If I get back and opened another object the first object will open and the problem is that its a sessionScoped bean so on the second opened it will not invoke the @PostConstruct. So what is the solution for forcing the sessionScoped to read the new value and open another session? Or how can I read an object by listener rather than @PostConstruct?

page1.java

@ManagedBean
@SessionScoped
class pageBean{

    MyObject myObj;
    public String save(){FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(myObj,"obj");
    }
}

@ManagedBean
@SessionScoped
class pageBean{
@PostConstruct
public void init()
MyObject = (MyObject)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("obj");
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
pFace
  • 81
  • 3
  • 10

1 Answers1

2

Your managed beans should have been @ViewScoped. Otherwise, you'd need to obtain it in (action)listener method such as <f:event>:

<f:event type="preRenderView" listener="#{bean.init}" />

wherein you obtain the session object in init() method.

public void init() {
    obj = (Obj) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("obj");
}

Or maybe lazily loaded in a getter if your sole purpose is to access it in EL:

public Obj getObj() {
    if (obj == null) {
        obj = (Obj) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("obj");
    }

    return obj;
}

But this makes no sense as the session scoped object is just available directly in EL by #{obj} without the need for an intermediating bean such as #{bean.obj}.

At least, the whole design makes no sense. That's why I suggested that they should actually have been @ViewScoped instead.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i know that i must use the viewScope but my problem with it is that it has conflict with the ui:composition and ui:insert nad ui:include and i can't remove them so i am obliged to use sessionScope my question is how can i make a call in another managedBean on page open (while navigating to the page) where in this way i can tell the managedBean to take another object rather than the object it has – pFace Jan 11 '13 at 13:40
  • Oh well yes, that is what I thought, but I'm too slow :) – Fallup Jan 11 '13 at 13:40
  • ok so what can i do now i think using a listner where the opened page takes the event but i don't no well how using this :S – pFace Jan 11 '13 at 13:48
  • As said, perform the job in (action)listener method (e.g. `` or in getter, if necessary by lazy loading. But doing this in getter makes no sense as session attributes are already directly available in EL by their name like `#{obj}`. – BalusC Jan 11 '13 at 13:56
  • i will try reading the object in getter but can i know how to use the and what its uses – pFace Jan 11 '13 at 14:00
  • its correct i used to read the object send by sessionmap in the getter and it worked i am so thakfull i will try the freRenderView maybe it will help me in another things but does takes a listener like this where it calls this method before opening the page? – pFace Jan 11 '13 at 14:23
  • sorry for repeating my question : does takes a listener like this where it calls this method before opening the page? – pFace Jan 11 '13 at 14:27
  • Yes, just write code exactly as shown in my answer. See further also http://stackoverflow.com/questions/6377798/what-can-fmetadata-and-fviewparam-be-used-for for other appliances of ``. – BalusC Jan 11 '13 at 14:28