• 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 validate - how-to repopulate lists, bean:write, etc. after validation fails

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever the user imputs fail the validation method in my form, I send the user a error defined in the properties bundle. This works flawlessly. However, when I return to the jsp, all of my non-user-entered, dynamically passed information is missing. For example: a dropdown list populated by an ArrayList of application objects, username field passed from the Action, etc. What should I do in order to get this information back to the jsp?? I know that the form instance that was used to send the info from the action to the jsp is nuked, so what should I do??
thanks,
A.T.
afouty@trinity.edu
[ October 23, 2002: Message edited by: A.T. Thomas ]
[ October 23, 2002: Message edited by: A.T. Thomas ]
 
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
In your struts-config.xml file, what is the scope of the action? Sound like you have it set to "page". Should be at least "request".
 
Ranch Hand
Posts: 567
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is one that bugged me for weeks after I started using struts.
You need to populate the dropdown lists with data that you only get during the request, and the place to do this is in the action bean.
So every request has to go through an action to get the data for the dropdowns.
Therefore, do not specify *.jsp files in your action mappings' "input" attribute. Specify another action. I generally have 2 action mappings for each action. For instance registerVote and registerVoteStart.
registerVoteStart does no validation. So it always goes through the action. This is also useful for the first call to registerVote, it means I don't have to program a switch in the validate method to avoid validating when I call the form at first.
Adam
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are placing complex objects onto your Form Beans that are being used on a JSP with validation, you will have to put the Form Bean on session, and not on the request.
When the validation framework inside of struts determines that one of your fields violates a validation rule, it appears that struts creates a new instance of the of form bean and repopulates the data that it can. (i.e.-primitives and strings.)
This will solve your problem.
 
David Yutzy
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
Adam, I've never really thought about using the "input" attribute of an action in that way...
So your saying that if you specify another action as "input", you make your DB calls there (versus your main action servlet) and pass them to the main servlet where they are used to populate the form bean? So someplace in your main action servlet you must forward to the JSP file, right?
We're specifying the JSP as our "input" and the servlet populates from DB, then to the form bean.
[ October 30, 2002: Message edited by: David Yutzy ]
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right.
If my validation fails, the request is forwarded to the action mapping declared in "input".
This action mapping declares no validation and forwards the request to my action object.
This action object does the DB access and presuming it's successful, forwards it to the JSP which is declared in the struts-config as the forward for this mapping.
 
David Yutzy
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
Hmmm, sounds reasonable, but for anything but smaller projects, wouldn't that create a huge amount of action servlets and huge struts-config.xml action section?
[ November 03, 2002: Message edited by: David Yutzy ]
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, been away a while. There's only one action servlet, and the subclasses of the org.apache.struts.action.Action are just classes, not servlets.
My struts-config.xml does get pretty big, but it's never caused any problems. The xml layout is adequate enough for huge lists of action mappings.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I�ve used an approach where validate is set to false in struts config so the action will always be called. Once in the action the validate method on the form is manually called. The action checks to see if any errors are returned. If there are errors, the action then puts any required objects back on the form or in the request. The JSP is now any to find everything that is needed to render.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought of doing it that way too. Since though I'd programmed an action for the first call to the URL anyway, which gets all the dropdown lists and forwards to the JSP, it's no extra effort to use that in the "input"-attribute for failed validation.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am interested in this very problem and have been thinking about how best to deal with it in a fairly generic manner.
All of my actions follow the pattern described above whereby the call to action invokes a FormAction that loads whatever i need into the request and then passes control to the jsp (or tile in my case) which holds the form UI components. pressing submit calls a handler action. of course any failed validations will bounce back to the first action, reload the data and repopulate the form. because the two events exist in separate requests it has seemed that the only logical place to put data common to both requests is in the session. I find this solution less than satisfactory however as it then becomes the responsibility of the final handler action to clean up any temporary cruft left in the session.
also this is problematic in cases where you may be wishing to implement multi-page forms.
the solution I am considering is inventing a new pseudo-scope, called 'pipe' that functions exactly as a session, except it is opened by the first action and ultimately closed by the final action. the pipe is a form specific singleton, ie there is one pipe per form-name.
A new Pipe would be created (or optained) via

and then attributes can be set and get just like a session or request.

and finally a pipe can be closed

actions (in their execute method) and forms (in their reset method), mid-pipe, can simply get the pipe for that form by calling

internally the pipe is simply a trimmed down interface similar to the standard HttpSession interface with an inner Factory class to handle the building and gathering.

From looking in the Struts1.1 source code at It does appear that it is valid to specify a 'name' param in your Action configuration in struts-config.xml without specifying an actual form, and thus you can use the name to tie actions and forms together in this manner.
Can anyone see any significant problems with this approach?
dave
 
dave sag
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dave sag:
From looking in the Struts1.1 source code at It does appear that it is valid to specify a 'name' param in your Action configuration in struts-config.xml without specifying an actual form, and thus you can use the name to tie actions and forms together in this manner.


needless to say I did not look hard enough. if you specify a 'name' the Struts controller assumes there is a form associated with that page and throws a wobbly.
so use the parameter instead and if already using it, like I am then I guess it's just a matter of formalizing your parameters. in my case

will need to become

doh. I'll post to this thread again when I have a proper working example of this. anyone interested in seeing the generica package i am developing should check out
the JavaDocs.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Adam! That tip was just what I needed, and I learned something. Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic