This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to keep variable value alive in JSF

 
Andrew Mike
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to update the entity using its id, when I hit the update(h:commandButton) my id value in the bean is lost. How to keep its value alive till my action complete.

BaseEntity.java


UserHome.java



User.xhtml



When i hit update button my page reloads before an action and the resulting User.xhtml without an id. This makes me to save a user name instead of update. How to acheive the correct functionality?
 
Zachary Pry
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew.

Problem is that your Managed Bean is @RequestScoped which mean it will last only for: "Request scope persists during a single HTTP request in a web
application
". In your case you should use:
View (@ViewScoped): View scope persists during a user's interaction with a single page
(view) of a web application.

OR
Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web
application.


View scoped managed bean will last only while you interact with current page(say index.xhtml). When you redirect to another page (say about.xhtml) - new Managed Bean will be created and old one will be garbage collected.
Session scoped managed bean will last about 30 minutes (it is default value, if I remember correctly).
 
Shasi Mitra
Ranch Hand
Posts: 101
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try having this "isIdDefined" in a hidden variable so that it is available for the next request..If you are using myfaces you can save its state. Or you can put just this variable alone in session scope. There are so many options. Dont worry
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18008
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shasi Mitra wrote:There are so many optionsXXXXX kludges.


Annotations imply JSF2. In JSF2, it's usually simpler to use View Scope.

I should point out, in addition, that the EL is all wrong here. You shouldn't be tagging the action value as a method call (with parentheses). The mere fact that it's an action means that the EL target must be a method accepting no arguments and the parentheses aren't needed to drive the point home.

Likewise, "rendered="#{!isIdDefined}. Which doesn't provide a bean/property pairing, such as "#{userHome.isIdDefined}", is missing the closing double-quote, and would actually be attempting to invoke a property-get method named isIsIdDefined.
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to give you a tip that will save you countless hours of aggravation. Use
@ViewAccessScoped - all you need to do is add the dependency...

http://myfaces.apache.org/extensions/cdi/
 
Yosi Pramajaya
Ranch Hand
Posts: 46
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if your data is very very important, just use @ApplicationScope

It will survive all day long, ended in the end of application.
Or you can use the database to integrated with.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic