This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm trying to populate a multi-selection html:select list in Struts. It works fine except for one thing. If I change the selection and try to submit and have the form validation fail, I loose any changes within the list. All other fields on the form are saved correctly.
This is the code I'm using.
I don't have a reset method in the form (although I initialize the array containing the selected values to a zero-length array in the declaration) and it doesn't get touched in the validate method. The only methods that influence the value for the array are its getter and setter.
The first thing I'd do to debug this is put a System.out.println inside the setUsers() method on the form bean to see whether it's being called or not. If it is being called, there's something funny going on in your form bean. If it's not getting called, check the spelling to make sure the property name in the jsp matches the setter name on the form bean.
If these approches don't work, show us: -The relevant portions of the form bean -The relevant portions of struts-config.xml -The jsp <html:form> tag.
Thanks for the help. The problem was with the struts config file.
It turned out that I was forwarding back to the initialization action for the page when validation failed. The list was the only field that I was loading from a database on that page so it was getting reinitialized everytime validation failed.
I have a question regarding this approach. If an action fwd is used as the "input" value of an action in the case of an error,(i.e., when a form validation fails, fwd to a someActionClass.do instead of somepage.jsp) when struts forwards to this new action, how do you get a handle on the ActionErrors object created upon the form validation method call?
Joined: May 17, 2001
Hi, Just found the answer to my question, looks like struts puts the ActionErrors into the request, so a call to servletRequest.getAttribute("org.apache.struts.action.ERROR"); will return the ActionErrors object.