On 11/25/05, Wendy Smoak <[EMAIL PROTECTED]> wrote:
>
> This is probably a JSF question, but I'm already here. :)
>
> MailReader's BaseViewController has:
>
> protected Object getBean(String name) {
> FacesContext context = getFacesContext();
> return context.getApplication().getVariableResolver().
> resolveVariable(context, name);
> }
>
> protected FacesContext getFacesContext() {
> return FacesContext.getCurrentInstance();
> }
>
> Unwinding everything, I get:
>
> FacesContext.getCurrentInstance().getApplication()
> .getVariableResolver()
> .resolveVariable(FacesContext.getCurrentInstance(), name);
>
> All that, just to find my session scoped managed bean? Or am I making
> it way more complicated than it needs to be? I *really* miss
> 'session.getAttribute()' right about now...
How do *you* know that it's a session scoped bean (versus perhaps being in
some other scope)?
How do *you* know whether it's been created yet, or whether it might get
created on demand (as a result of this evaluation) as a managed bean?
There's more to getBean() than just resolving a reference to an existing
session scope attribute, and Shale has an analogous method in its base class
that should really be used here :-). If you want simpler -- but still
portable between webapps and portlets -- go with:
FacesContext.getCurrentInstance
().getExternalContext().getSessionMap().get(name);
but you give up on the bean being in *any* scope, and being created on
demand.
If you really really want a session scoped attribute in a webapp, you can do
that too:
HttpSession session = (HttpSession) FacesContext.getCurrentInstance
().getSession();
return session.getAttribute(name);
--
> Wendy
Craig