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.
getBoolean
returns true. – Needful