| From | Sent On | Attachments |
|---|---|---|
| Legolas Woodland | Sep 21, 2006 4:07 pm | |
| Alexandre Jaquet | Sep 21, 2006 11:32 pm | |
| Craig McClanahan | Sep 22, 2006 1:42 am | |
| K. Johnson | Sep 22, 2006 8:37 am |
| Subject: | Re: how to access a managed bean from a button action listener ? | |
|---|---|---|
| From: | Craig McClanahan (crai...@apache.org) | |
| Date: | Sep 22, 2006 1:42:18 am | |
| List: | org.apache.myfaces.users | |
On 9/21/06, Legolas Woodland <lego...@gmail.com> 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





