-1

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 '???'

nimo23
  • 5,170
  • 10
  • 46
  • 75

2 Answers2

0

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

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Yes, normally this would work. But I am using weld and hence the instance cannot be retrieved by this – nimo23 Oct 15 '12 at 13:05
  • 1
    have you looked at `getSessionMap()` , take a look what keys and values are there... might give you a hint – Daniel Oct 15 '12 at 13:13
0

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

nimo23
  • 5,170
  • 10
  • 46
  • 75