users@javaserverfaces.java.net

Re: JSF 1.2/ Facelets/ Managed Bean Scope

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Thu, 04 Oct 2007 15:09:57 -0700

Butash, Bob wrote:
> Yes, digging into the source code we discovered this is happening.
> However, don't you think it is unneeded overhead. I have to get an
> object, there by instantiating in this case a list but it could be a
> complex object containing additional complex objects, just so I can
> throw it away and assign a instance obtained from the model layer to the
> bean name. For performance reasons we do not like to make a habit out
> of unnecessarily instantiating objects to just throw them away.
You're not instantiating it to throw it away. When you call
getValue(), the bean
(be it a list or a complex object) will be pushed into its scope. When
you call setValue(),
it will use the object that was just pushed to scope by the getValue() call.
> Nor is
> it good practice to hard code the scope of the managed bean into the
> java code, since this eliminates the ability to manage the scope in a
> single location, the faces config.
>
Fair enough. My comment about the lack of a scope prefix was to explain
why the result went to request scope instead of session scope. Nothing
more.
> Was there a reason why this behavior changed from 1.1 to 1.2?
>
Most likely an oversight by the EG.
Please log an issue against the spec:
https://javaserverfaces-spec-public.dev.java.net
> Thanks
>
> -----Original Message-----
> From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
> Sent: Wednesday, October 03, 2007 7:32 PM
> To: users_at_javaserverfaces.dev.java.net
> Subject: Re: JSF 1.2/ Facelets/ Managed Bean Scope
>
> 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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net
>
>