views:

286

answers:

1

Hi. I have a j2ee application running on weblogic. I was confused with my multibox. WHta I know of multibox is the checked items will be passed as array of strings on submit. I don`t know why in my application it works fine when i uncheck a checkbox or more but as long as a single box remains checked. when i uncheck everything what will be submitted will be an array of the previously checked multiboxes when it is supposed to be empty. help me please

+2  A: 

Are you familiar with the reset() method on the ActionForm class?

The purpose in life for this method is to reset checkboxes. If you have a checked checkbox in your form and you submit it, that checkbox will be on the request. If the checkbox is unchecked nothing will be sent on the request for it (a GET submit is a simple way to observe this behavior).

When Struts performs the request bind, it matches by name the parameters from the request to the parameters in the form. That is, if there is something to match.

Now consider these steps:

  • I have a boolean field on the ActionForm;
  • I also have a matching checkbox in the form;
  • I submit the form => Struts binds the request, so now my property is true in the ActionForm;
  • I uncheck the checkbox in the form and submit again => nothing is sent on the request for the checkbox => Struts has nothing to bind = > your field remains true on the ActionForm;

The above applies for multi checkboxes, but you get an array instead of just one value.

Enter the reset() method. This is called by Struts before binding the request. Here you can set your field value to false. If it arrives in the request Struts will replace it with true => OK. If it does not arrive on the request (because it's unchecked) the value will remain false = > OK again.

The same goes for multiboxes. You have to reset the list of values from the ActionForm by reducing the array to zero length (but not null).

If your ActionForm has a request scope, it usualy does not matter because the object is recreated at each request. But for a session scoped ActionForm with checkboxes, reset() is a must.

dpb