• Post Reply Bookmark Topic Watch Topic
  • New 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

Uninitialize a property when unselecting data from html:optionsCollection

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When I select from a system list(populated using optionsCollection ) it works very well. For instance, systemname[0] = '21' if I select the 1st system in the list. Problem is when I unselect that system. systemname[0] is still = '21' when it should be = to '' or null

When I trace the code, I can see the setSystemName() being executed when I select a system. But the same method isn't executed when I unselect the system I selected before. So, systemname[0] is still = '21' and nothing is selected on the window. How do I get around that problem when I unselect all data in a optionsCollection? I don't want to use the reset button for this. Below I added code from the formbean and jsp page.


thanks.

In struts-config, action for the search form..
<action path="/smtSearch" type="gov.mdot.webapp.smt.control.action.SearchAction"
name="SearchForm"
input="/jsp/Search.jsp"
delegater="gov.mdot.webapp.smt.delegate.SearchDelegateImpl"
scope="session"
validate="true" >
<forward name="success" path="/jsp/Search.jsp"/>
</action>


// formbean
private String systemName[]

// jsp page
<TD align="left" valign="top">
<html:select property="systemName" size="10" multiple="multiple">
<html:optionsCollection name="SearchForm" property="systemList"/>
</html:select>
</TD>
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to ask Struts-related questions in the Struts forum. This topic has been moved there for you.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This problem is due to a quirk in HTML. If the user selects no options in a <select> input, nothing gets sent back to the server for that input, and hence Struts doesn't call the setter.

In this case where your ActionForm is in session scope, you must override ActionForm's reset() method, and in that method you must set the systemname array to an empty array. Example:

Struts calls the reset method prior to calling the setters. Therefore, if the user does select an option, the setter will be called and will replace the empty array with the values selected.
 
Francois Bourgault
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that and it works. But what if I added errors in the ActionErrors class in the validate() method? The user will see all error messages. Will he/she need to re-enter all the data again?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Francois Bourgault:
Will he/she need to re-enter all the data again?


No. Here is the order in which Struts calls methods on your ActionForm class:

1-reset()
2-the setters (setX(), setY(), etc.)
3-validate()

When you clear data in your reset method, it's immediately re-populated with what the user entered when Struts calls the setters. By the time the validate method is called, the fields are populated properly, and if the JSP is redisplayed because of an error, it will show the data the user entered.
 
Francois Bourgault
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works. Thanks for the clarification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic