Reputation: 308
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.
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.
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.
Upvotes: 7
Views: 3968
Reputation: 524
Try setting the form class's boolean property using your modal in the action class itself, the one struts will be calling before your jsp is loaded
Upvotes: 1
Reputation: 845
In Struts, a class property cannot be accessed through a struts when the Getter method takes an argument (which I believe does not follow the JavaBean specification). Mapped property or indexed property functionality (the () and []) can be used only for submitting data, not for displaying fields.
So, the only way to get the checked attribute is to use JSP scriptlets, to do this you will have to define the form object in the JSP page (<bean:define id="form" name="<form name, as it is in struts-config.xml>"/>
), and than you will be able to write something like this:
<html:radio property='<%=\"value(" + questionBase + "B)"%>' <%=form.getBoolean(questionBase) ? "checked" : "" %> value="No">No</html:radio>
.
Or you can use JSTL.
Upvotes: 1
Reputation: 3431
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);
}
Upvotes: 1