Eclipse or SUNrise...

Eclipse or SUNrise...
...JAVA for sure

Tuesday, March 11, 2008

STRUTS and checkbox subimttion problem

There is a common problem with checkboxes and the STRUTS library - when unchecked in the form, their values are not populated properlly in the ActionForms (mostly booleans). There is couple of resolutions to this problem, but the best one in my opinion is to add aditional HTML input tag just after the checkbox tag:
<html:checkbox property="option" value="true" />
<input type="hidden" name="option" value="false">

How does it work? Well, the value of the hidden field as you know is always submited - so in the GET header it is visible as
yourURL?option=false

but when we check the checkbox (on) its value is submited as well, so our header goes like this:
yourURL?option=true&option=false

As you can see the option parameter is now duplicated in the header, but the header parser is reading only the first occurance of the parameter so our ActionForm is populated with the TRUE value.
Using this solutions is safe. All browsers shouldn't have any problems with it. It also doesn't depends on logic layer inside i.e. Action Classes witch makes the UI part of the application more transparent and transitable.