• 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

Retaining List Values After Validating - Is there a way?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I am using the validator plug-in for my page validations and it works fine and I can get my error message to display on my page. My Lists however are not coming back after the validation. My jsp has a non-static picklist on it which is populated with a List.
I have scoured the newsgroups and have found that other people have had the same problem. The issue seems to be that the form bean only retains the values of the Strings and Primitives after the validation. The Lists are no longer on the request when the form is rebuilt. I understand all that, no problem. My question is...how do I get around this?

- I have heard people suggest putting the form or even just the lists in the session (my team lead isn't fond of cramming stuff in the session).

- I have read that some people rebuild the lists after validation by calling the DB again. This doesn't seem like something that should be done in the Form...? Plus the users selections would be lost and they would be forced to fill out all of the fields on the jsp again. As a user that would be very annoying.

I am thinking that this has to be something that has caused people grief in the past. What is the best way of handling Lists with validation? Has anyone found a way...?

Thanks for any insight you might have!
Janis
[ November 07, 2006: Message edited by: Janis L Goldsmith ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have heard people suggest putting the form or even just the lists in the session (my team lead isn't fond of cramming stuff in the session).


I'm not fond of "cramming stuff in the session" unnecessarily either, but I'm certainly not afraid to use the session when I need to. In my opinion most developers have gone way overboard in trying to avoid using the HTTPSession object. It's there to be used, and putting small to moderate-sized lists in it is not going to negatively affect performance. The key is to make liberal use of the sesssion's removeAttribute() method to clean up objects once you're done with them.

- I have read that some people rebuild the lists after validation by calling the DB again. This doesn't seem like something that should be done in the Form...? Plus the users selections would be lost and they would be forced to fill out all of the fields on the jsp again. As a user that would be very annoying.


I'm not sure where you got the idea that rebuilding your lists would cause the input data to be lost, but it's not correct. ActionForm has a reset() method that you can override that gets called by Struts after a submit occurs, but before validation takes place and before the setter methods are called. Hence, it's a good place to put logic to rebuild lists of data by re-querying the database.

My point is that either:

- putting the ActionForm (or at least the data lists) in the session or
- leaving everything in request scope and rebuilding the list data by re-querying the database

is a valid option for dealing with this problem.
[ November 07, 2006: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm...wasn't this question just asked?

https://coderanch.com/t/55068/Struts/Drop-down-jsp

I have read that some people rebuild the lists after validation by calling the DB again. This doesn't seem like something that should be done in the Form...? Plus the users selections would be lost and they would be forced to fill out all of the fields on the jsp again. As a user that would be very annoying.


That is what I do...though I have my input point back to an action. I load the lists in the action class, not in the form. Since this all happens on one request cycle, the user's current selections and entered values are repopulated when the page is shown (as long as you don't clear them in your action class).

- Brent
 
Janis L Goldsmith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for the reply.

I too think that using the session in moderation isn't that bad of a thing. Maybe I'll have to push a little harder for that solution ;-)

I'm still not positive that the rebuilding option will work the way I think. One example of using a list that I have on my form is a pick list where the user will add/remove values and then submit the new lists. If validation fails for some other field then I don't want to rebuild the picklist from the DB, I want to redisplay their selections so they don't have to do it all over again. Is this possible using the reset() method as you suggest? The lists come back null from the form in the request. Would I just use request.getParameterValues() in the reset method perhaps?

What about the idea that DB calls do not belong in the form object? I have read that some people believe that it "violates" the struts design. Would my code be considered hokey to make the DB calls there if they really belong in an Action class?

Thanks for your input, it's really appreciated.

Janis
 
Janis L Goldsmith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hmmm...wasn't this question just asked?



Sorry if this was just covered but..

Drop down boxes do not seem to be as much of a problem because you retain the user's selected option as primitive. This picklist of mine seems to be a little trickier because there is no way of retaining the user's selections without using the request.getParameterValues(). I can do this but should it be done in the Form or does something like this belong in the Action class? And...is this a hack or just the normal way of getting around this?

Thanks again,
Janis
[ November 07, 2006: Message edited by: Janis L Goldsmith ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what other do, but I would regard code in your reset method that makes business layer calls or retrieves data from the database a hack. As a general practice I declare all my forms as request scope so I pretty much never have a need to code a reset method. Storing forms in session is fine, but I would advocate that the reset method should only be used to reset the properties back to the default state.

For an "edit" type page I often have three different action mapping definitions that only differ by the parameter attribute. I will have parameter values of "add", "edit" and "refresh". It is the "refresh" mapping that I use to show the page when validation fails. It is in the refresh branch of code that I would add any special logic to rebuild your picklist. Note that at this point your form should be populated with all the values that the user entered on the screen, so I don't see why you would need to call request.getParameterValues().

The issue seems to be that the form bean only retains the values of the Strings and Primitives after the validation. The Lists are no longer on the request when the form is rebuilt. I understand all that, no problem.


Do you truly understand this? Why is it that lists not on the request but Strings are? It may help if you understand what is going on. When your html page is shown in the browser, the request that displayed that page is gone and everything on that request goes away. Now when you submit your page a new request is created and the only parameter values that will be populated on the request are the ones submitted as part of your form. This is basically just the input fields on your form. In the case of a select field, just the value of the selected items is submitted, not the entire list. You could store the contents of your lists in hidden input fields on your form and then you would not have to retrieve them again, but that approach adds a bit of complexity and a lot more data that needs to be transferred across the Internet...generally not a good idea.

- Brent
 
reply
    Bookmark Topic Watch Topic
  • New Topic