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
Joined: May 05, 2006
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 ]
Joined: Sep 27, 2002
Thank you for your reply. Regarding your last solution. When there is an error where do I forward the request to?
Joined: May 05, 2006
It depends on how you set it up, but you can have it forward to the initial read action.