You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Legolas Woodland <le...@gmail.com> on 2006/09/22 01:07:31 UTC
how to access a managed bean from a button action listener ?
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
Re: how to access a managed bean from a button action listener ?
Posted by Alexandre Jaquet <a....@avintis.com>.
actionListener="#{session.actionToExecute}"
in your managed bean create
public void actionToExecute(ValueChangeEvent event) {
//String value = ( String) event.getNewValue();
}
Legolas Woodland 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
>
>
Re: how to access a managed bean from a button action listener ?
Posted by Craig McClanahan <cr...@apache.org>.
On 9/21/06, Legolas Woodland <le...@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
[1] http://shale.apache.org/