webtier@glassfish.java.net

Re: [webtier] Using ui:include with View Scoped beans

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Mon, 15 Feb 2010 09:26:58 -0800

On 2/11/10 2:12 AM, webtier_at_javadesktop.org wrote:
> I am only new to JSF but I have found myself stuck on a problem for quite some time and I am not sure if it is purely down to lack of knowledge to the lifecycle of the view scope.
>
> The scenario is that I want to use a view scoped bean to store the state of a component. There is one property on the bean that I want to read out and that is of the path for the currently displayed page (path to the file such as /blah/foo.xhtml). I use it in the following manner:<ui:include src="#{MyViewBean.viewPage}"/>
>
> There are some buttons on the page that change the viewPage property on MyViewBean which would cause the value of viewPage to change and thus a different component would be rendered. This is because I don't know which page to display at design time.
>
> The behaviour that ensured was that no matter what buttons you clicked (made sure the request was not causing a redirect) the MyViewBean bean was being created every single request. I even created a @PostConstruct annotated method, set a breakpoint there and could confirm a new view scoped variable was being created every request.
>
> If I changed MyViewBean to be SessionScoped, it would behave as expected however I wanted the behaviour to be view based so two browser windows could co-exist in which the view scope fits the bill!
>
> After removing the<ui:include/>, using the debugger, I could confirm that the view scoped bean being was being managed correctly and that a new instance was not being created every request as expect.
>
> After some digging around, it looks like my issue starts with the way the order the view scope is set in the restore view phase of JSF. Turns out, the EL expressions are executed before JSF restores the view scope. The consequence is that when the FaceletViewHandlingStrategy.buildView() is run, only session scoped beans are available and thus JSF creates a new view scoped object and uses that.
>
> Is this the intended behaviour of view scopes or should I be looking at creating my own custom scope?
>
Do you have components within your view using the binding attribute in
concert with view scoped beans?

> Thanks in advance,
>
> -- Marc
> [Message sent by forum member 'mdergacz' (marc_at_skytix.com.au)]
>
> http://forums.java.net/jive/thread.jspa?messageID=386057
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>
>