File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How to keep variable value alive in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to keep variable value alive in JSF" Watch "How to keep variable value alive in JSF" New topic

How to keep variable value alive in JSF

Andrew Mike

Joined: Aug 04, 2011
Posts: 5
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.


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

Joined: Aug 09, 2011
Posts: 18
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
". 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.

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

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).

Open-source JDBC Library - Midao JDBC ( ).
Shasi Mitra
Ranch Hand

Joined: Nov 27, 2008
Posts: 101

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

Joined: Jun 25, 2001
Posts: 17410

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.

An IDE is no substitute for an Intelligent Developer.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
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...
Yosi Pramajaya
Ranch Hand

Joined: Jun 30, 2011
Posts: 46

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.

Independent Programmer - Technology Enthusiast - JUG Leader from JUG Joglosemar
I agree. Here's the link:
subject: How to keep variable value alive in JSF
It's not a secret anymore!