Posts Archives

Thursday, July 21, 2005

Getting 'unchecked' value from Struts checkbox

The following was posted last year on a different blog. I'm consolidating posts here and you never know when this info may come in handy :)

Having just spent half the afternoon trying to get a page with a checkbox to work correctly with Struts I stumbled across the following. I knew that an unchecked value was never returned in the form and, as we are using Struts DynaActionForms, was starting to think about over-riding the Form when I came across a solution that is so simple it's embarrassing.

The solution is to add a hidden field immediately after the checkbox field i.e.

<html:checkbox property="currentLocation" value="true"/>
<html:hidden property="currentLocation" value="false"/>
When the form is submitted if the checkbox is selected the hidden field is ignored, but if the checkbox is empty it isn't in the form so the hidden field is used.
Works a treat.

Comments:
Thanks for excellent code snippet. I was wondering to use reset method on view bean but this worked and saved half a day for me
 
I wasted around a day but this simple code has helped me a lot
Thanks
Khasim
 
Post a Comment

Links to this post:

Create a Link



<< Home



Powered by Blogger