Question

I have a very simple issue. Below is my JSP code , where I display multiple check boxes in rows. By default all checkbox are checked, and if user wants to ignore certain row, he unchecks that row's checkbox.

My issue is, if a row's checkbox is unchecked and because of some other fields, if Struts throws a form validation error on screen, the unselected checkbox is displaying back as checked. Am I missing something? If no struts validtion, then everything looks correct.

<c:forEach var="map" items="${form.displayList}" varStatus="index"> 
    <html-el:checkbox styleId="someCheckbox_${index.index}" property="someCheckboxes[${index.index}]"  />
     <td> .....</td>
     <td> .....</td>
     <td> .....</td>
</c:forEach>
Was it helpful?

Solution

When you submit data to Struts the following things happen:

  • Struts has a front controller and all request are going through this controller. This is the ActionServlet class which then based on what was configured in struts-config.xml passes the request to the appropriate Action class.
  • before that happens, an ActionForm instance is constructed or reused if it's a session saved form;
  • reset() is called on the ActionForm. This is where you reset checkbox properties to false. This is needed because unchecked HTML checkboxes are not sent on the request when you submit, so Struts does no request data binding for them (if they were checked when you loaded the page they remain checked even if the user unchecked them on submit);
  • the ActionForm is populated with the request data (any checked checkboxes are now checked, the ones who were not sent on the submit remain unchecked because of the previous reset);
  • the ActionForm's validate() method is called if so specified in struts-config.xml;
  • if validation fails, the control is returned to the input JSP if so specified in struts-config.xml (the submitted data is what the page will display because it's on the ActionForm);
  • if instead validation is successful, the execute() method on the Action class is called;
  • Action class does it's job and forwards to some JSP.

Is this how you are using Struts? My guess is you are doing some populating/resetting/validations in the Action class and when validation fails you reload the ActionForm with the default data.

Check your ActionForm for the reset() and validate() code and your Action class for execute() and see where the data is getting checked back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top