Meaningless Drivel is fun!*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
Author

How to keep variable value alive in JSF

Andrew Mike
Greenhorn

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.

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

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


Open-source JDBC Library - Midao JDBC ( www.midao.org ).
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: 15632
    
  15

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
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...

http://myfaces.apache.org/extensions/cdi/
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: http://aspose.com/file-tools
 
subject: How to keep variable value alive in JSF
 
Similar Threads
Problem in Editing Table Data
Edit DataTable
Struts2 multiple files upload with a twist
Problem Persisting Entity In Hibernate
Problem With the "action" Attribute