Radio Buttons Are Not "Marked" in Struts 1 ActionForm

I have a JSP page with several questions and an ActionForm with a map of names and input values.

When I load the page, the values ​​(checked attribute) for the radio inputs arent checked, but there are checkboxes.

The form defines:

<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 accessed via the load / form submit by getBoolean / setBoolean .

The following correlation methods are available in my ActionForm class:

 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 am using struts 1.2.7, Hibernate 3, DisplayTag 1.0, OpenJDk 6 and Tomcat 6 on Ubuntu 14.04.

Update

However, the following do radio inputs 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 at boot.

Update (5/10/15)

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

+7
java java-ee jsp forms struts-1
source share
3 answers

Firstly, the boolean value accepts only 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 is better to use an autoboxed Boolean object instead of a boolean. Thus, the value must be set to your Boolean so that it contains the value.

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

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

What do you think can happen when your logic element always gets false ? public void setBoolean (String key, boolean) {values.put (key, value? "yes": ""); }

Something like this might seem like the best solution.

 Map<String, Boolean> values = new Hashmap<String,Boolean> public void setBoolean(String key, Boolean value) { values.put(key, value); } 
+1
source share

In Struts, a class property cannot be obtained through struts when the Getter method takes an argument (which, I believe, does not meet the JavaBean specification). The properties of the associated property or indexed property (() and []) can only be used to send data, and not to display fields.
So, the only way to get the checked attribute is to use JSP scripting scripts, for this you will need to define a form object on the JSP page ( <bean:define id="form" name="<form name, as it is in struts-config.xml>"/> ) and you can write something like this: <html:radio property='<%=\"value(" + questionBase + "B)"%>' <%=form.getBoolean(questionBase) ? "checked" : "" %> value="No">No</html:radio> <html:radio property='<%=\"value(" + questionBase + "B)"%>' <%=form.getBoolean(questionBase) ? "checked" : "" %> value="No">No</html:radio> .
Or you can use JSTL.

+1
source share

Try setting the boolean property of the form class using your modal code in the action class itself, one column will be called before loading jsp

+1
source share

All Articles