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 validation design question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts validation design question" Watch "Struts validation design question" New topic
Author

Struts validation design question

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Hi All,

I have an action called CreateNewUserPrepareAction. The jsp has a User form to capture the data. In adition to this, the JSP has a list box whose items are retrieved from the database. The values for the list box are stored as a List in the request as an attribute and are then displayed as part of the form.

When the user submits the form the request gets sent to CreateNewUserAction. I validate the contents using the Validate method. If the user has entered some invalid entry I return to the page displaying an error message. However, the items for the list box are no longer in the request(which is correct). I do not want to store them in the session as I dont think they belong there.

Has anyone come across this problem before and can the recommend some pattern that provides a nice solution. I dont think it is correct either if the Validate method sends a call to the database either.

Thank you
David
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
This is a very common issue. Here are some possible solutions:

1. Put the list in session (worst idea)

2. Put the list in application scope (not a horrible idea if the list is static)

3. Repopulate the list in the form's reset() method. (Violates MVC and is not a place where another developer would look to see where a list is populated)

4. On error, forward to the initial read action with an error flag, making sure to only populate the list in the read action if the error flag is set. (It is not a best practice to forward to an action)

5. Extend Action and have a populateList(ActionForm form) method and abstract ActionForward doAction(). Make sure all of your actions extend this new class and all of the logic is in the doAction method. In the execute() method of this class, call populateList(form) before calling the doAction() method. Overwrite populateList in the actions that have a list. That is the best solution that I can come up with.


Let me know if you have any other questions.
[ June 29, 2006: Message edited by: Dom Lassy ]
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Thank you for your reply.
Regarding your last solution. When there is an error where do I forward the request to?
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
It depends on how you set it up, but you can have it forward to the initial read action.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts validation design question