• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reset values in back form bean

 
Jorge Martinez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy,

When we have a jsf form to store clients in a ddbb for example and the backing bean is in session scope.... Lets think of a user that requests 3 times the xhtml view with the form to store 3 clients.

1) If we dont reset the backing bean each time the form is processed, he would see the values he sent beforea and it doesnt have much sense, right?

2) Here there is an example of clearing form after submitting : http://stackoverflow.com/questions/6975755/clear-form-content-after-submitting. Clearing the form after submititng is a common practice for the reason of the statement 1, right?

3 ) What is the best or normal approach to reset values in a form? when the user requests it or done automatically by the application after processing a form?

4) Are there known issues about resetting or not resetting the values in a form bean?

Thanks
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

The view scope for the managed bean is what you really want. As you experienced, under session scope, the form values remain after submission.

Under view scope, the bean resets itself "every time" the JSF view is loaded because the bean's (default) constructor is called. Under view scope, if you have a "reset" button then you will need to explicitly reset each form value.

Depending on the behavior of your pages, after submission you can not return to that same page, that way you may avoid the form values issue (I may be wrong).
 
Jorge Martinez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy,

Yes, always the action method of the form returned other view it would be solution but I am thinking of an example where you send the form and you want to stay in the same view in order to send again over and over so I think here other solution would be to have a request scoped bean, right?

Thanks
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request scope is useless.

I learned a long, long time ago - long before GUIs - that the best time to initialize data isn't when you get done doing something, it's when you start doing it the next time. That way you know that the data is really initialized and don't have to trust what "someone else" did.

So here's what my code would typically look like:



JSFUtils is just a class I defined to hide away useful JSF-specific functions instead of putting then as hard-coded login in all my beans. In this example, the 2 methods I invoke just add messages for the FacesContext.
 
Jorge Martinez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy,

But then in the case we have, I mean, just a form to store clients for example and not navigating to other page:

1. Why not use request scope beans and clear the form after an operation like we should do if we used view scoped beans?

2. So your approach would be view scope for forms like this one, right?


Thanks
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't "store stuff in a form" in JSF. The form is just the means to display and/or change what's in the backing bean. JSF is oriented towards the server side.

Request scope really is just about totally useless in JSF.

View scope would be good as long as only one View was involved, but you'd still need to blank out the backing bean (init) for each new record you add, since View scope is just an automated way of deleting a Session scope object when you are done using that View.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!