• 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

How to keep variable value alive in JSF

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 101
Spring Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27752
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

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.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 48
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic