0

I am confused about expression language 2 bean-view communication syntax.First of all.Is it possible to have one more managed bean with same name but different scobes.if it is what about accessing them via expression language on the other hand; consider there is an attribute in session called DemoBean and there is a session Scobed managed bean called DemoBean as well, if I try to access this bean via EL like this #{DemoBean} // refers bean or attribute? they say #{sessionScobe} is the way to access session attributes but
just #{sessionattributename} is valid ? and when I put an object to the session map is it referanced or copied?

daemonThread
  • 243
  • 2
  • 12

1 Answers1

1

JSF's aim is to manage session attributes itself for you. That means you should forget about keeping/recovering session map attributes manually, because you'll be able to do it using JSF's mechanisms.

When you want to keep some info for the whole application, use @ApplicationScoped annotation into your managed bean. You can do the same for sessions, requests or concrete views, using @SessionScoped, @RequestScoped and @ViewScoped annotations.

So, if you want to store some attribute in session:

@ManagedBean
@SessionScoped
public class SessionScopedBean{

    public String sessionAttribute;

    //Getter and setter

}

You can recover/change the value of the attribute of the bean using the FacesContext:

FacesContext context = FacesContext.getCurrentInstance();
SessionScopedBean bean = (SessionScopedBean) context.getApplication()
    .evaluateExpressionGet(context, "#{sessionScopedBean}", SessionScopedBean.class);
bean.setSessionAttribute("value");

Remember JSF beans are by default named with bean's name with the first character lowercased. And remember also this bean will be null at the first hit if you don't initialize yourself. You can do it in an easy way using a PreRenderViewEvent in the bean you want to initialize and executing it for example in your login page. That ensures the bean will keep alive for the rest of the session.

Finally, that's the way you'll access your session attribute from your view:

#{sessionScopedBean.sessionAttribute}

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Thanks for advices but "when I put an object to the session map is it referanced or copied?" – daemonThread Apr 07 '13 at 17:17
  • As java references its objects it will be referenced. So if you change object's value it will be also changed into the map (at the end, the session attributes are also stored in a map). However and as I said, get rid of it when using JSF. – Aritz Apr 07 '13 at 20:32
  • When new session scobed managed bean created does it implicitly added to sessionmap? so accessing it via req.getSession().getAttribute("credentials"); is true? I am asking this because I have login filter and session scobed managedbean called credentials.In filters dependency injection is not allowed and FacesContext#getExternalContext().getSessionMap().get("credentials"); returns null we cant access faces context in filter so only way seams session attribute i guess. – daemonThread Apr 07 '13 at 21:54
  • Yeah, in a filter that should do the job, as JSF beans are stored in session map in an attribute with their name. Remember that if this returns null it's probably because it's not initialized. Just make a call to some bean's method or initialize it manually before calling the filter. – Aritz Apr 08 '13 at 06:44