Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio buttons don't have "checked" in Struts 1 ActionForm

I have a JSP page with a number of questions and an ActionForm with a Map of input names and values.

When I load the page, the values (checked attribute) of the radio inputs aren’t set, but the checkboxes are.

Form define:

<bean:define id="form" name="questionForm" type="com.example.QuestionForm"/>

Radio (jsp):

<html:radio property="<%=\"boolean(\" + questionBase + \"V\" + respLabel + \")\"%>" styleClass="pepperoni1" value="1"  >Yes</html:radio>
<html:radio property="<%=\"boolean(\" + questionBase + \"V\" + respLabel + \")\"%>" styleClass="pepperoni0" value="0"  >No</html:radio>

Radio (html):

<input type="radio" class="pepperoni1" value="1" name="boolean(pepperoniVNever)">Yes
<input type="radio" class="pepperoni0" value="0" name="boolean(pepperoniVNever)">No

Checkbox (jsp):

<html:checkbox property="<%=\"boolean(\" + questionBase + \"V\" + respLabel + \")\"%>" styleClass="pepperoni" />

Checkbox (html):

<input type="checkbox" class="pepperoni" checked="checked" value="on" name="boolean(pepperoniV1)">

The checked attribute is not set but the values are not empty when being access via on page load/form submit by getBoolean/setBoolean.

My ActionForm class has these correlating methods available:

public void setValue(String key, String value) {
    if (value != null)
        values.put(key, value);
}

public String getValue(String key) {
    return values.get(key);
}

public boolean getBoolean(String key) {
   if (values.get(key) != null){
        if(values.get(key).equalsIgnoreCase("1")){
            return true;
        } else if(values.get(key).equalsIgnoreCase("0")){
            return false;
        }
        return "yes".equalsIgnoreCase(values.get(key));
    }
    return false;
}

public void setBoolean(String key, boolean value) {
    if(value){
        values.put(key, "yes");
    }
}

I'm using struts 1.2.7, Hibernate 3, DisplayTag 1.0, OpenJDk 6, and Tomcat 6 on Ubuntu 14.04.

Update

However, the following radio inputs do work (as you can see by the "checked" attribute:

Radio (jsp):

<html:radio property="<%=\"value(\" + questionBase + \"B)\"%>" value="No">No</html:radio>
<html:radio property="<%=\"value(\" + questionBase + \"B)\"%>" value="Yes">Yes</html:radio>

Radio (html):

<input name="value(noteB)" value="Yes" checked="checked" type="radio">
<input name="value(noteB)" value="No" type="radio">

But after editing the inputs using boolean to use value, the checked attribute is still not set on load.

Update (5/10/15)

After making the changes recommended by shinjw, the values are saved correctly (which was a separate issue) but the checked attribute is still not set for some of the radio buttons when getBoolen returns true.

like image 867
Andrew Breksa Avatar asked Apr 28 '15 17:04

Andrew Breksa


People also ask

How do I get radio buttons checked?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

What is the difference between radio button and check box?

Checkboxes and radio buttons are elements for making selections. Checkboxes allow the user to choose items from a fixed number of alternatives, while radio buttons allow the user to choose exactly one item from a list of several predefined alternatives.


1 Answers

Firstly, booleans only take true or false. Not 0 or 1

<html:radio property="<%=\"boolean(\" + questionBase + \"V\" + respLabel + \")\"%>" styleClass="pepperoni1" value="true"  >Yes</html:radio>
<html:radio property="<%=\"boolean(\" + questionBase + \"V\" + respLabel + \")\"%>" styleClass="pepperoni0" value="false"  >No</html:radio>

It would be better to use the autoboxed Boolean object instead of boolean. In that way a value has to be set to your Boolean in order for it to contain a value.

Doing this will also reveal a logical issue in your code.

public void setBoolean(String key, boolean value) {
    values.put(key, value ? "yes" : "");
}

What do you think might happen when your boolean is always getting set to false? public void setBoolean(String key, boolean value) { values.put(key, value ? "yes" : ""); }

Something like this might pose as a better solution

Map<String, Boolean> values = new Hashmap<String,Boolean>

public void setBoolean(String key, Boolean value) {
    values.put(key, value);
}
like image 117
shinjw Avatar answered Sep 28 '22 15:09

shinjw