• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@ViewScoped not working

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JSF2 on Tomcat 6 with Spring 3.

My manged bean is:


I have an editQuestion.xhtml page. As i click any action button on page e.g. Cancel, Save Question etc the manged bean is created again. In JSF 1 we achieved it usign keepAlive but @viewScoped is supposed to replace it in JSF 2.

I have to use @Component because i need to inject some spring beans in managed bean and spring does not recognize @ManagedBean.

 
anyz mick
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be issue with Spring. Because Spring scope is "request" it makes it created every time. I tried removing the spring scope then ViewScoped bean is never created again even i navigate to differnt pages.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a fundamental JSF principal that you cannot inject a short-scoped object into a longer-scoped one. In particular, you cannot inject Request-scope objects into Session-scoped ones. ViewScope is a variant of Session Scope with a longer lifespan than Request Scope and a shorter lifespan than full Session Scope. ViewScope objects can therefore only be injected with View, Session, and/or Application scope objects. If you inject a Request Scope object into it (assuming JSF doesn't complain), the injected Request scope object will be invalid the first time the backing bean is updated on postback.

Normally, my Spring Beans are effectively at Application Scope, not Request scope. I say "effectively", since if the JSF bean gets serialized, all bets are off, but that's something I hope JSF will fix some day and it doesn't matter here.

For Request-scope objects, I use JSF, not Spring. I may inject Spring objects into the Request-scope objects, but the objects themselves are JSF_managed, not Spring-managed.
 
anyz mick
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for detailed explaination.

I created the custom Spring scope as guided at this webpage.

So @ViewScoped bean now contains objects of same scope.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic