You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Bobby Rosenberger <br...@gmail.com> on 2005/11/18 17:57:28 UTC

Accessing a session-scoped backingbean

Hey guys,

I would like to access a session scoped backingbean ("user") from an
actionListener... I'm currently doing something along the lines of:

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)context.getExternalContext().getRequest();
HttpSession session = request.getSession();
UserBean user = (UserBean)session.getAttribute("user");

Is there a better way?

Thanks,

Bobby Rosenberger

Re: Accessing a session-scoped backingbean

Posted by Don Tam <dt...@consumercontact.com>.
Hi Bobby,

use valuebindings.
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        ValueBinding vb = 
app.createValueBinding("#{ApplicationVariablesBean}");
        ApplicationVariablesBean avb = (ApplicationVariablesBean) 
vb.getValue(context);

Bobby Rosenberger wrote:

> Hey guys,
>
> I would like to access a session scoped backingbean ("user") from an 
> actionListener... I'm currently doing something along the lines of:
>
> FacesContext context = FacesContext.getCurrentInstance();
> HttpServletRequest request = 
> (HttpServletRequest)context.getExternalContext().getRequest();
> HttpSession session = request.getSession();
> UserBean user = (UserBean)session.getAttribute("user");
>
> Is there a better way?
>
> Thanks,
>
> Bobby Rosenberger
>

-- 
Don Tam
Manager, Software Development
(416)493-6111x143
dtam@consumercontact.com


Re: Accessing a session-scoped backingbean

Posted by Mario Ivankovits <ma...@ops.co.at>.
Simon Kitching wrote:
> Or do you mean that using the faces-config.xml file to specify 
> dependency injection is better than explicitly looking up stuff using 
> faces-specific code?
+1
> I did list the "injection" solution first on the wiki in the hope it 
> would prod people towards using that solution :-)
I noticed it :-)


Thanks!
Mario


Re: Accessing a session-scoped backingbean

Posted by Simon Kitching <sk...@obsidium.com>.
Mario Ivankovits wrote:
> Mike Kienenberger wrote:
>> Thanks.  I hadn't considered the possibility that this wasn't a 
>> managed bean.
>> So if it's an unmanaged bean, it sounds like it's better to work with
>> the Map directly, but if it is a managed bean you probably don't want
>> to do that since the bean may not exist yet.
>>   
> But as long as the bean is managed, I dont understand why all this 
> resolver stuff is recommended instead of the manager-property stuff?
> As long as you are able to avoid any faces specific stuff in your bean 
> its easier to reuse such a bean, isnt it?

Do you mean that it seems easier to just grab the object from the 
request or session map than look it up via the myfaces variable 
resolver? As was noted, doing a lookup in the map will not trigger 
creation of the bean; you get null if it doesn't already exist. 
Sometimes this is desirable, sometimes not.

Or do you mean that using the faces-config.xml file to specify 
dependency injection is better than explicitly looking up stuff using 
faces-specific code? I certainly agree with this, with the point that it 
keeps the target class free of JSF-specific code being a very important 
one. But that's a matter of taste that not everyone shares; both 
approaches are technically valid. I did list the "injection" solution 
first on the wiki in the hope it would prod people towards using that 
solution :-)

Cheers,

Simon

Re: Accessing a session-scoped backingbean

Posted by Bobby Rosenberger <br...@gmail.com>.
I just re-read my last post (along with the resulting responses) and,
besides the typo I found in my post, realized that my statements were too
general. I hope I didn't offend. What I meant to say was that I liked Craigs
idea better "for my current problem".

The help on this list has been great. I didn't intend to convey anything
negative.

Bobby

P.S As for updating the wiki FAQ - I feel inadequate, as I've never done it
before, but I'd be willing to help out. Someone would have to point me in
the right direction... and provide a map. ;-)

On 11/18/05, Mario Ivankovits <ma...@ops.co.at> wrote:
>
> Mike Kienenberger wrote:
> > Thanks. I hadn't considered the possibility that this wasn't a managed
> bean.
> > So if it's an unmanaged bean, it sounds like it's better to work with
> > the Map directly, but if it is a managed bean you probably don't want
> > to do that since the bean may not exist yet.
> >
> But as long as the bean is managed, I dont understand why all this
> resolver stuff is recommended instead of the manager-property stuff?
> As long as you are able to avoid any faces specific stuff in your bean
> its easier to reuse such a bean, isnt it?
>
>
> ---
> Mario
>
>

Re: Accessing a session-scoped backingbean

Posted by Mike Kienenberger <mk...@gmail.com>.
The resolver stuff is part of the managed property stuff.

You'd only use it when you have no way to get a reference to a managed
bean, or it's inconvenient to get a reference to a managed bean using
dependency injection.

For instance, I used them in converters and servlet filters.

On 11/18/05, Mario Ivankovits <ma...@ops.co.at> wrote:
> Mike Kienenberger wrote:
> > Thanks.  I hadn't considered the possibility that this wasn't a managed bean.
> > So if it's an unmanaged bean, it sounds like it's better to work with
> > the Map directly, but if it is a managed bean you probably don't want
> > to do that since the bean may not exist yet.
> >
> But as long as the bean is managed, I dont understand why all this
> resolver stuff is recommended instead of the manager-property stuff?
> As long as you are able to avoid any faces specific stuff in your bean
> its easier to reuse such a bean, isnt it?
>
>
> ---
> Mario
>
>

Re: Accessing a session-scoped backingbean

Posted by Mario Ivankovits <ma...@ops.co.at>.
Mike Kienenberger wrote:
> Thanks.  I hadn't considered the possibility that this wasn't a managed bean.
> So if it's an unmanaged bean, it sounds like it's better to work with
> the Map directly, but if it is a managed bean you probably don't want
> to do that since the bean may not exist yet.
>   
But as long as the bean is managed, I dont understand why all this 
resolver stuff is recommended instead of the manager-property stuff?
As long as you are able to avoid any faces specific stuff in your bean 
its easier to reuse such a bean, isnt it?


---
Mario


Re: Accessing a session-scoped backingbean

Posted by Mike Kienenberger <mk...@gmail.com>.
Thanks.  I hadn't considered the possibility that this wasn't a managed bean.
So if it's an unmanaged bean, it sounds like it's better to work with
the Map directly, but if it is a managed bean you probably don't want
to do that since the bean may not exist yet.

On 11/18/05, Craig McClanahan <cr...@apache.org> wrote:
>
>
> On 11/18/05, Mike Kienenberger <mk...@gmail.com> wrote:
> > Why is pulling the bean from the session map better than asking the
> > application for it using
> >
> facesContext.getApplication().getVariableResolver().resolveVariable("neededBean")
> > or facesContext.getApplication
> ().createValueBinding("#{neededBean}").getValue(facesContext),
> > which is what Don suggested?
>
>  The latter two methods will work *if* the bean already exists, *or* if
> you've defined "user" as a managed bean as well.  In that case, it will do
> the normal "create this bean for you if it does not exist" trick, as well as
> looking it up for you.
>
>  Using the Map is better if you do *not* want to trigger a managed bean
> creation.  But all of these methods are feasible.
>
> > Wouldn't it be possible that a custom variable resolver may not store
> > the bean in the session map under that name?
>
>  That would be technically feasible, but it would break the contract for the
> default VariableResolver defined in the spec ... that doesn't seem like
> something you would want to have happen.
>
> >   Or perhaps there's some
> > other reason why this might not work in some alternate JSF
> > implementation.  It just seems like you're working too close to the
> > internals by reading it out of the map yourself.
>
>  Actually, the Map in question is *required* by the spec to act the way it
> does.  In fact, it's not a "real" Map at all (in the sense of something like
> HashMap or TreeMap) ... it is an abstraction that, underneath the covers,
> calls session.getAttribute(), session.setAttribute(), and so on.  The reason
> a Map representation was chosen was to hide the fact that the underlying
> session object is an HttpSession if you're running in a standard webapp, or
> a PortletSession when you're running in a portlet.
>
>  Craig
>
>

Re: Accessing a session-scoped backingbean

Posted by Craig McClanahan <cr...@apache.org>.
On 11/18/05, Mike Kienenberger <mk...@gmail.com> wrote:
>
> Why is pulling the bean from the session map better than asking the
> application for it using
> facesContext.getApplication
> ().getVariableResolver().resolveVariable("neededBean")
> or facesContext.getApplication
> ().createValueBinding("#{neededBean}").getValue(facesContext),
> which is what Don suggested?


The latter two methods will work *if* the bean already exists, *or* if
you've defined "user" as a managed bean as well. In that case, it will do
the normal "create this bean for you if it does not exist" trick, as well as
looking it up for you.

Using the Map is better if you do *not* want to trigger a managed bean
creation. But all of these methods are feasible.

Wouldn't it be possible that a custom variable resolver may not store
> the bean in the session map under that name?


That would be technically feasible, but it would break the contract for the
default VariableResolver defined in the spec ... that doesn't seem like
something you would want to have happen.

Or perhaps there's some
> other reason why this might not work in some alternate JSF
> implementation. It just seems like you're working too close to the
> internals by reading it out of the map yourself.


Actually, the Map in question is *required* by the spec to act the way it
does. In fact, it's not a "real" Map at all (in the sense of something like
HashMap or TreeMap) ... it is an abstraction that, underneath the covers,
calls session.getAttribute(), session.setAttribute(), and so on. The reason
a Map representation was chosen was to hide the fact that the underlying
session object is an HttpSession if you're running in a standard webapp, or
a PortletSession when you're running in a portlet.

Craig

Re: Accessing a session-scoped backingbean

Posted by Mike Kienenberger <mk...@gmail.com>.
Why is pulling the bean from the session map better than asking the
application for it using
facesContext.getApplication().getVariableResolver().resolveVariable("neededBean")
or facesContext.getApplication().createValueBinding("#{neededBean}").getValue(facesContext),
which is what Don suggested?

Wouldn't it be possible that a custom variable resolver may not store
the bean in the session map under that name?  Or perhaps there's some
other reason why this might not work in some alternate JSF
implementation.  It just seems like you're working too close to the
internals by reading it out of the map yourself.

On 11/18/05, Craig McClanahan <cr...@apache.org> wrote:
>
>
> On 11/18/05, Bobby Rosenberger <br...@gmail.com> wrote:
> > Hey guys,
> >
> > I would like to access a session scoped backingbean ("user") from an
> actionListener... I'm currently doing something along the lines of:
> >
> > FacesContext context = FacesContext.getCurrentInstance();
> > HttpServletRequest request =
> (HttpServletRequest)context.getExternalContext().getRequest();
> > HttpSession session = request.getSession();
> > UserBean user = (UserBean)session.getAttribute("user");
> >
> > Is there a better way?
>
>  Yes ... your way binds you to the servlet API and will not work if you ever
> port the app to be a portlet.  Try this instead:
>
>      FacesContext context = FacesContext.getCurrentInstance();
>      Map map = context.getExternalContext().getSessionMap(); // Returns map
> of session attributes
>      UserBean user = (UserBean) map.get("user");
>
>  There are corresponding maps for request scope attributes and application
> scope attributes, plus a bunch of other useful information, available
> through the ExternalContext.  Note also that the attribute maps are
> writeable as well as readable ... doing a map.put() operation on the map
> returned above will add the corresponding object as a session scope
> attribute in the underlying servlet or portlet session.
>
> > Thanks,
> >
> > Bobby Rosenberger
> >
> >
>
>  Craig
>
>

Re: Accessing a session-scoped backingbean

Posted by Craig McClanahan <cr...@apache.org>.
On 11/18/05, Bobby Rosenberger <br...@gmail.com> wrote:
>
> Hey guys,
>
> I would like to access a session scoped backingbean ("user") from an
> actionListener... I'm currently doing something along the lines of:
>
> FacesContext context = FacesContext.getCurrentInstance();
> HttpServletRequest request =
> (HttpServletRequest)context.getExternalContext().getRequest();
> HttpSession session = request.getSession();
> UserBean user = (UserBean)session.getAttribute("user");
>
> Is there a better way?


Yes ... your way binds you to the servlet API and will not work if you ever
port the app to be a portlet. Try this instead:

FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getSessionMap(); // Returns map of
session attributes
UserBean user = (UserBean) map.get("user");

There are corresponding maps for request scope attributes and application
scope attributes, plus a bunch of other useful information, available
through the ExternalContext. Note also that the attribute maps are writeable
as well as readable ... doing a map.put() operation on the map returned
above will add the corresponding object as a session scope attribute in the
underlying servlet or portlet session.

Thanks,
>
> Bobby Rosenberger
>
>
Craig

Re: Accessing a session-scoped backingbean

Posted by Mario Ivankovits <ma...@ops.co.at>.
Bobby Rosenberger wrote:
> I would like to access a session scoped backingbean ("user") from an 
> actionListener... I'm currently doing something along the lines of:
...cut...
> Is there a better way?

Yes, there are.
Please see http://wiki.apache.org/myfaces/FAQ "How can I access one 
Managed Bean from another?"

Personally I prefer the "managed-property" way.

---
Mario


Re: Accessing a session-scoped backingbean

Posted by Bobby Rosenberger <br...@gmail.com>.
Fantastic! Thanks guys!

I always ask Mr. Google (smartest guy I know ;-)) before posting here... but
I missed the wiki FAQ.
After reading it, I think like Craig's solution better. Perhaps it should be
added to the FAQ?

Anyway, thanks again.

Bobby Rosenberger.

On 11/18/05, gramani@intellicare.com <gr...@intellicare.com> wrote:
>
>
> craigmcc@gmail.com wrote on 11/18/2005 12:09:16 PM:
>
> >
> > Actually, that method *does* work as well, if your backing bean
> > extends Shale's AbstractViewController base class.
>
> umm.. well, ok... I just figured I must be wrong seeing as you didn't
> choose that answer..! (I guess your answer covers the more general JSF way,
> yes..?)..
>
> >
> > Craig
>
> Geeta
>

Re: Accessing a session-scoped backingbean

Posted by gr...@intellicare.com.
craigmcc@gmail.com wrote on 11/18/2005 12:09:16 PM:

> 
> Actually, that method *does* work as well, if your backing bean 
> extends Shale's AbstractViewController base class.

umm.. well, ok... I just figured I must be wrong seeing as you didn't 
choose that answer..! (I guess your answer covers the more general JSF 
way, yes..?)..

> 
> Craig

Geeta

Re: Accessing a session-scoped backingbean

Posted by Craig McClanahan <cr...@apache.org>.
On 11/18/05, gramani@intellicare.com <gr...@intellicare.com> wrote:
>
>
> Sorry, please ignore what i said.. didn't read your question
> properly..sorry!
> Geeta
>

Actually, that method *does* work as well, if your backing bean extends
Shale's AbstractViewController base class.

Craig

Re: Accessing a session-scoped backingbean

Posted by gr...@intellicare.com.
Sorry, please ignore what i said.. didn't read your question 
properly..sorry!
Geeta

Re: Accessing a session-scoped backingbean

Posted by gr...@intellicare.com.
Bobby Rosenberger <br...@gmail.com> wrote on 11/18/2005 11:57:28 
AM:

> Hey guys,
> 
> I would like to access a session scoped backingbean ("user") from an
> actionListener... I'm currently doing something along the lines of:
> 
> FacesContext context = FacesContext.getCurrentInstance();
> HttpServletRequest request = (HttpServletRequest)context.
> getExternalContext().getRequest();
> HttpSession session = request.getSession();
> UserBean user = (UserBean)session.getAttribute("user");
> 
> Is there a better way?

If you use Shale, (and if your backing bean extends 
AbstractViewController), then I believe you can simply say 
getBean("user");

> 
> Thanks,
> 
> Bobby Rosenberger
> 

Regards,
Geeta