Search code examples
htmlstruts-1struts-html

Can two radio tags be used to set boolean values for a form property in struts?


I'm new-ish to struts and I'm particularly stuck on an area of struts code which has to do with the radio button. No matter what I do I can't get anything but a false value from the following: (CostForm)

<td align="left" width="200px" colspan="2">
    <html:radio property="responsableBool" value="false"/>No
    <html:radio property="responsableBool" value="true"/>Yes
</td>

It is then initialised from this piece of code:

CostForm costform = (CostForm) form;
Cost cost = new Cost();
costform.populateModel(cost);

and the populateModel just has: PropertyUtils.copyProperties(cost,this);

The only thing I can think of is that struts doesn't allow the radio buttons to reference the same property with different values.


Solution

  • Given the form:

    public class CostForm extends ActionForm {
        private boolean responsableBool; // And getter/setter
    }
    

    The HTML:

    <html:form action="/costsub">
        <html:radio property="responsableBool" value="false"/>No
        <html:radio property="responsableBool" value="true"/>Yes
        <html:submit/>
    </html:form>
    

    The action:

    public ActionForward execute([args elided]) throws Exception {
        CostForm costForm = (CostForm) form;
        System.out.println(costForm.isResponsableBool());
        // etc.
    

    When I click "No" and "Yes" I get the expected boolean value inside the action.

    I'd double-check things like spelling (the English spelling would be "responsible"; perhaps it's spelled correctly in Cost?), action/form mismatches (are you using the right form "name" in the action mapping?), and so on.