• 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 Validator and Dynamically Populated Select Lists

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking for additional information on techniques for populating select lists from a database when using the Struts Validator. Note that I am somewhat new to Struts, and am currently using Struts 1.2.9.

As I would assume is common in the industry, I need to load select options from a database to use in my form. In many examples, doing this type of thing is often shown in an action method, where the necessary options are loaded and put into the request before forwarding to the JSP page.

The problem arises when you introduce Struts Validation. If a validation fails, the action's execute method never gets called, and thus can't be used to load data into the request for use by the form.

I found one page online that addresses this issue:

http://www.learntechnology.net/content/struts/validate_manually.jsp

This page was a welcomed find, but I have to say that I didn't like any of the proposed solutions. To those that have dealt with this problem, what kind of options did you consider, and what resources or web sites did you find that talked about it?

Further Thoughts:

The fact that the execute method never gets called when there is a validation failure I think makes sense, which makes me question whether the action method is really where setting up the next page should be taking place. Why should the current page's action have to do anything more than accomplish the requested task and decide what page to go to next, without having to do setup for that next page? Instead of going from an Action method to a JSP page, why isn't there a step inbetween to perform any necessary setup for the JSP page? Not only would this potentially fix this validation issue (validation could then skip the action execution but the setup method for the JSP would still be called), it would also potentially reduce code duplication when multiple different actions return to the same JSP page (though this can also be mitigated by having a seperate setup method for such JSP pages within something like a DispatchAction class).

Thanks,

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

The problem arises when you introduce Struts Validation. If a validation fails, the action's execute method never gets called, and thus can't be used to load data into the request for use by the form.


My general thought is that every page needs two actions: one action to prepare the data needed to display the page and one page needed to process the information from the page. This is not always the case but it probably applies in this situation. So how does this tie into your situation and the quote above? If you have an action that pulls data from the database to show the page, then it is likely that when a validation error occurs you want to call this action again. You can do this by specifying the "display" action as the input attribute. You need to specify the ".do" part of the action...like this:
input="/RefreshUser.do"

Note the name of the action above..."Refresh". I often have multiple action mappings that use the same underlying action class but that specify a different parameter attribute. For example I might have "AddUser", "EditUser", and "RefreshUser". AddUser knows that it needs to set default values in the form. EditUser know that it needs to retrieve a record from the database. RefreshUser knows that it should use the values from the populated form.

I just took a look at the link that you mention. That article does not mention my approach. The setUp method approach seems like a decent solution as well.

- Brent
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic