File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts Validator and Dynamically Populated Select Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Validator and Dynamically Populated Select Lists" Watch "Struts Validator and Dynamically Populated Select Lists" New topic
Author

Struts Validator and Dynamically Populated Select Lists

Scott Arnold
Greenhorn

Joined: Oct 24, 2007
Posts: 6
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
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts Validator and Dynamically Populated Select Lists