Question

Within my (JSF, Servlet 3.0) sessionscope there are instances of classes. How can I get the instance of a class by using the names provided in sessionscope?

For example, in my session there is a instance of MyBean:

classes-ManagedBean-class com.MyBean=Bean: Managed Bean [class com.MyBean] with qualifiers [@Any @Default @Named]; Instance: com.MyBean@40a6d41f

In java code, I want to retrieve the actual instance of MyBean:

HttpSession session = us.getHttpSession();
MyBean mybean = (MyBean) session.getAttribute("???");

Which value should I provide for '???'

Was it helpful?

Solution

this what you after :

FacesContext context = FacesContext.getCurrentInstance();
MyBean mybean =  (MyBean) context.getExternalContext().getSessionMap().get("myBean");

Note that myBean is the name of the managed bean

for example @ManagedBean(name = "myBean ") , if no (name = "myBean ") is specified the default will be the same as class name with first letter in lower case

OTHER TIPS

The above is retrieved from SessionMap. I made a workaround by putting a key explicitly in sessionmap again. Now it works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top