开发者

Can`t uncheck all in Struts Multibox

开发者 https://www.devze.com 2022-12-21 18:15 出处:网络
I have a j2ee application running on weblogic. I was confused with my multibox. What I know of multibox is that the checked items will be passed as an array of strings on submit.

I have a j2ee application running on weblogic. I was confused with my multibox.

What I know of multibox is that the checked items will be passed as an array of strings on submit.

I don`t know why in my application it works fine when i uncheck a checkbox or more, as long as a single box remains checked but when I uncheck everything, the submitted array is the array of the previously checked multiboxes when it was开发者_JAVA百科 supposed to be empty.

Can you help me please?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号