users@javaserverfaces.java.net

Re: JSF 1.2/ Facelets/ Managed Bean Scope

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Wed, 03 Oct 2007 16:31:48 -0700

Butash, Bob wrote:
> Sure....
>
> *Managed Bean:*
> <managed-bean>
> <managed-bean-name>movieSearchResultsList</managed-bean-name>
> <managed-bean-class>java.util.ArrayList</managed-bean-class>
> <managed-bean-scope>session</managed-bean-scope>
> </managed-bean>
>
> *Backing Bean Logic:*
> public String quickSearchAction() throws Exception
> {
> FacesContext context = FacesContext.getCurrentInstance();
>
> context.getApplication().createValueBinding("#{movieSearchResultsList}").setValue(context,
>
> obtainMovieBusinessService().findMoviesByTitle(getSearchText()));
> return "searchResults";
> }
>
> With this example the movieSearchResultsList is placed into request
> scope not session scope.

This is happening because you've called setValue() without calling
getValue(), so
your managed bean hasn't been created and thus can't be resolved.

The ManagedBeanELResolver will take no action in the setValue() case, it
instead
allows the ScopedAttributeELResolver to handle it. The
ScopedAttributeELResolver will
create a new attribute if the specified attribute (in this case
moveSearchResultsList) can't be
resolved, and since the attribute isn't prefixed with a scope, the scope
it pushes the new attribute
to will be request.

The managed bean facility will only push a bean into scope in the
getValue() case.

So change your code to:

public String quickSearchAction() throws Exception
{
    FacesContext context = FacesContext.getCurrentInstance();
    ValueBinding vb =
context.getApplication().createValueBinding("#{movieSearchResultsList}");
    vb.getValue(context);
    vb.setValue(context,
obtainMovieBusinessService().findMoviesByTitle(getSearchText());
    return "searchResults";
}

and it should work. Of course, you can bypass the setValue() call
altogether and cast
the getValue() result to a List and add the value your self.



>
> Thanks
>
> ------------------------------------------------------------------------
> *From:* Jason Lee [mailto:jason_at_steeplesoft.com]
> *Sent:* Wednesday, October 03, 2007 10:30 AM
> *To:* users_at_javaserverfaces.dev.java.net
> *Subject:* Re: JSF 1.2/ Facelets/ Managed Bean Scope
>
> I'm not sure I followed all of that. Can you send the relevant
> snippets from your faces-config.xml and any Java code?
>
> On 10/3/07, *Butash, Bob* < bob.butash_at_eds.com
> <mailto:bob.butash_at_eds.com>> wrote:
>
> I'm leveraging JSF 1.2 with Facelets and I'm running into an
> error.
>
> I have a backing bean the retrieves a list from the model tier
> of the application, I then try to set the list as the value of
> a configured managed bean. The managed bean is scoped to
> session scope however I notice that if I just set the managed
> bean without retrieving it first for some reason the managed
> bean's scope is set to request instead of session per it's
> configuration.
>
> The usecase
> 1. Leveraging JSF 1.2 and Facelets
> 2. Declare Managed Bean as session scope
> 3. Attempt to set the managed bean value without previously
> obtaining it from the managed bean creation facility
>
> With this scenario the managed bean is placed into request
> scope not session scope. If I retrieve the managed bean
> leveraging the managed bean creation facility, when I set it,
> it is properly set into session scope. However, it doesn't
> seem right to have a new list instantiated just so that I can
> throw it away. This did work in my JSF 1.1 application.
>
> Any help would be apprecriated.
>
> Thanks
>
>
>
>
> --
> Jason Lee, SCJP
> Software Architect -- Objectstream, Inc.
> JSF RI Dev Team
> http://blogs.steeplesoft.com
>