Bookmark Topic Watch 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic