On 9/21/06, Legolas Woodland <[EMAIL PROTECTED]> wrote:
Hi
thank you for reading my post
how i can access a managed bean from a button action listener ?
for example i have a managed bean named session and its scope is session
, i used it to store user details until he/she logout
and when he/she pressed the logout button i need to change some of that
managed bean property ,
can you please tell me how i can do it ?
I also need it for time that user press the login button.
thanks
From within an action listener, you can access a session scoped value named "foo" in a couple of different ways:
(1) Use the session attributes map in external context:
FacesContext context = FacesContext.getCurrentInstance();
Map sessionAttributesMap = context.getExternalContext().getSessionMap();
FooBean value = (FooBean) sessionAttributesMap.get("foo");
*** NOTE: This approach does not trigger managed bean
creation if the bean does not already exist.
(2) Use the managed bean mechanism programmatically:
FacesContext context = FacesContext.getCurrentInstance();
FooBean value = (FooBean)
context.getApplication ().getVariableResolver().resolveVariable("foo");
This will use the same managed beans creation logic that JSF uses when resolving expressions, so it will cause the bean to be created if needed.
If you're using Shale[1], by the way, there is a convenience method called getBean() in the abstract base class for view controllers that hides the grungy detail for you.
Craig
[1] http://shale.apache.org/

