JavaRanch Home    
 
This page:         last edited 21 December 2007         What's Changed?         Edit

Refresh Values On Validation Error   

Question:

I've got a JSP with a select tag and options generated from a database that I put in request scope. It works fine if there are no errors, but if Struts validation returns to the input page because of a validation error, the collection of options is no longer there. How can I solve this problem?


Answer:

Provided by Brent Sterling

This is happening because values placed in request scope are out of scope once a page has been submitted. The solution is to refresh these values prior to redisplaying the page.

Generally, a page needs two actions: one action to prepare the data needed to display the page and one action to process the information from the page. This is not always the case but it certainly applies in this situation.

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. Just remember 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 knows that it needs to retrieve a record from the database. RefreshUser knows that it should use the values from the populated form.



Return to StrutsFaq


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton