Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I get a viewScoped bean from a request bean?

 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to everybody,
How can I do that?
I tryed with these:

and


but it seams they don't work.
thanks
Massimo
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Take a look at this recent post: http://www.coderanch.com/t/588300/JSF/java/More-than-one-ManagedProperty

but be sure to take into account Tim's comments. The rule is that the scope of the property must be no less than
that of the containing bean, so if you were trying to do it the other way round it wouldn't work - if I've got it
right that is. Also if you were using CDI you'd simply @Inject one bean into the other.

Regards,
Brendan.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer,
I would like to add an other important detail.
In my request bean i don't want to create a new view scoped bean, but i would like to get a value that was previously setted.
thanks
Massimo
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what @ManagedProperty does - it's the JSF managed bean mechanism for injecting one bean into another and if I'm
reading this correctly does exactly what you want. I recently came across a scenario where someone was trying to use this
mechanism when using client state saving (javax.faces.STATE_SAVING_METHOD=client) and it wouldn't work but was ok
with javax.faces.STATE_SAVING_METHOD=server.



Regards,
Brendan.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer,
I have implemented the @ManagedProperty in my request bean and in this way I can use the viewSoped bean method, but I have noted that when i call the page linked to the requestScope bean, toghether with its constructor, it is also called the viewScoped constructor...so when I try to get the values that i had previously saved obviously I get an empty field.
In fact I "lose" the view and then the viewScoped is "distroyed".
It's possible to avoid this "distruction" and get those values or do exist some other technics ?
thanks
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18162
53
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects in View scope are destroyed when you switch to a different view. Other than that, they behave exactly the same as session scope. And in fact, as far as I know, that's what they really are: session-scope beans with an attached destruction mechanism.

So if you want to retain your bean and its state, promote it from ViewScoped to SessionScoped.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic