• 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

struts validation repopulate arrays, arraylists

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a JSP where i have to iterate over an Array/Collection to display my records.

In the ActionForm i am validating the input using XML validations.

Now, if the JSP form is submitted and the validator returns any errors, the JSP is not able to find this ArrayList that was set before.

Can anyone tell me why this is happening.?

Doesn't struts repopulate these arrays/Collections/Map that we iterate after validation?

is there any workaround for this?

Thanks all,
Seshu
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what point are you storing the ArrayList into the session?
 
Sree Jag
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array/collection is first set when the action file for the actionform executes.

the collection is not stored in session 'coz that collection is needed only for this jsp.

now when the JSP is submitted for validation, and if there are any errors, then it returns to the JSP but now Struts cannot find the array/collection that was set.

any ideas?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is probably that the action is not invoked at all if validation fails (the ActionForm is validated before it's passed on to Actions for processing).
 
Sree Jag
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah that's true. the action file will not be executed unless the validate() method succeeds.

But doesn't the actionform retain the previous values and display them when the validate() method fails? it does right?

but why isn't this happening with collections/arrays?

Seshu
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use an array instead of an ArrayList, does it work then?
Also, this thread at JGuru might help.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
If you use an array instead of an ArrayList, does it work then?



I doubt it. That link that you gave was right on though, I think. You'll see this problem a lot if you have your ActionForm scoped as "request" (the default). Each time a request comes in, a new instance of the form is created so anything that you did to the ActionForm in a previous request was done on a totally different object. It does not survive to the next request-response cycle. One other solution may be to set the ActionForm scope to session. Of course then you'll have to clear any fields whose values you don't to be "sticky".
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pl check if the array/collection is there in form.reset() method. If yes, remove it.
 
Sree Jag
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't have any fields in reset method. also, i tried setting the form bean's scope to session with no luck.

is there any other elegant solution to this?

did u guys come across this problem in your applications? if yes, how did u tackle it?


-seshu
 
tumbleweed and gunslinger
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a stab here but I read somewhere that you could specify an Action as the "input" for another action, instead of a JSP.

This would mean when the validation fails, it runs the Action, which could populate the dropdowns for you and redirect to the .jsp, instead of just back to your JSP page.

Never tried it, but is sounded possible. The point of the article was to create a standard "pre" population action that may be used across multiple pages, such as State, Country, or other common dropdowns.

Any thoughts or confirmation on this?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a definite possibility and there is another thread started by Leandro on the same topic.

For this case, though, you could place a getter method in the ActionForm that retrieves the collection from somewhere else. I like doing this as it meshes well with the logic:iterate tag.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, yes, the approach you mentioned is valid and a good one if you are talking about relatively static lists such as options lists for States and such. From what I can tell, this strategy may not apply very well to Seshagiri's problem.

Seshagiri, I have a pretty good idea of what's happening but I don't think it will help you if I explained it in general terms. Post your code (the code in the Action that populates the collection, and the portions in your JSP that iterate through the collection) so that I can explain to you what's going on and how you fix it.
 
Sree Jag
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc, i've done using approach similar to what to u have suggested. but isn't this a bug in struts. i think even the arrays/collections should be repopulated when the validation fails.

what do u think?
reply
    Bookmark Topic Watch Topic
  • New Topic