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

3

7

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.

Needful answered 28/4, 2015 at 17:6 Comment(11)
Could you please post the syntax for using radio button ?Acroter
Added syntax for the radio and checkboxNeedful
Can you post the code of radio button from HTML Source code once its populated at front end.??Lindbom
Is it a value issue? I can't tell from your wording. Which value? Unchecked?Bronchoscope
@DaveNewton The values for the radio buttons (button in example) are empty on submit. The checkboxes go through fine.Needful
I believe the issue is with the name. Please change this name boolean(pepperoniV1) to some thing else . Please use the same name as given in form.Acroter
@Uma_HS The question names aren’t declared as fields in the form, they are entries in a hashmap. "pepperoniV1" is the name in the hashmap.Needful
@Uma_HS I added the correlating methods from the ActionForm.Needful
@AndrewBreksa Not sure , Can you try changing value of radio options to true /false & make necessary changes in setBoolean method.Acroter
@Uma_HS: Will do here in a bit. I've debugged the application and the get/setBoolean methods are called correctly (no errors, correct parameters, and return values) on page load/submit.Needful
@Uma_HS I made the changes as per shinjw's answer, which fixed the form values being saved, but not the checked attribute being set when getBoolean returns true.Needful
D
1

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);
}
Disney answered 6/5, 2015 at 1:5 Comment(19)
Due to some of the questions (not posted here) values being strings, I can't change the HashMap, but I did change the get/setBoolean methods to work with 1/0 as well as the older "yes" value. Debugging shows that the get/setBooelan methods are returning the correct response. I'll update my question with the new methods.Needful
Your answer fixed the data not being saved, but the radio buttons still don't have the "checked" attribute when getBoolen returns true. Any thoughts?Needful
Well, you answered the bounty question as I thought the submission issue and the display issue were the same problem. Don't want it to go to waste. Any thoughts on the display issue?Needful
Can you clarify? Are you saying the form comes without a default select? Two empty radio buttons?Disney
The form should load with values previously specified through the form (the user can go back, forward, save and resume, etc), but the form loads with neither of the radio buttons in each group are selected, even though the getBoolen method returns a true or false for that group (radio button name) (seen via eclipse debugging)Needful
You can set the value using jquery.Disney
$('input:radio[name="value(noteB)"]').filter('[value="<%=yourscriptlethere%>"]').attr('checked', true);Disney
Or you could create these radio button within their own if/else scriptlet blockDisney
As in meilechh's answer?Needful
Correct. However using jquery would be a much more elegant solutionDisney
OK, I'll give it a try.Needful
Were you able to get it fixed?Disney
Not yet, working on finding a way to have the values set by jQuery, but have it only lookup the values for the elements on the page (wouldn't I need to set an array of checked radio inputs and hand those to the JavaScript?)Needful
You cannot set any parameters in the getboolean method. You will want to create another method that returns all the values in your mapDisney
Well, all of the check boxes work. I just need to get the radio inputs that aren't being checked when the page loads (as they should be, even though the getBoolean method is being called). I have over 10 sets of two radio inputs.Needful
But I have been able to set parameters in the getBoolean method, and it does get called and returns the correct value, just these sets of radio inputs don't get checked, even after the getBoolean method is called.Needful
Checked is either true or false. Not checked=checkedDisney
So I need to add "checked=true"? Or just "checked"?Needful
Let us continue this discussion in chat.Needful
R
1

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.

Roberge answered 11/5, 2015 at 3:21 Comment(2)
But the checkboxes (and some of the radio inputs) use the get/setBoolean methods successfully.Needful
I do have the form object defined, sorry for not including it in the question, updating now...Needful
O
1

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

Outspeak answered 11/5, 2015 at 3:45 Comment(12)
Tell me if I don't understand your answer, but the HashMap of values already is set by the Action class, which is what the get/setBoolean methods are pulling from.Needful
I'm not sure how <HTML:radio> calls form class's get Boolean method. I'd always design my form bean to have variables inside it as boolean radio1, boolean radio2 etc. And set its value during action class. The underlying assumption is that HTML:radio tag will invoke get"property attribute" value from form bean. So my form bean will be getRadio1 that returns a boolean. My HTML:radio's property attribute will be "radio1"Outspeak
Ok, that makes sense. I would, but the application I'm working with uses one ActionForm for multiple forms, hence the HashMap of key/values.Needful
OK just a small tip try adding a print line in the getboolean you used to check whether that was invoked. And Property attribute of html radio should be form Bean's variable nameOutspeak
Used Eclipse's debugger to watch, looks like the get/setBoolean methods are being invoked and returning true/false correctly.Needful
You say "checked attribute is not set for some of the radio buttons" if I'm not mistaken then I think its because after returning the value from the getter the property attribute is responsible for naming and grouping the radio buttons together. Since you've given a common name for all the radio buttons, I think they've all been grouped together. After the page has been rendered, try selecting a radio button from different groups to see whether they work as different groups or work as oneOutspeak
Yes, there are grouped radio buttons (there are actually ~20 sets of radio buttons on the form). Why would the noteB grouped (if I'm using that right) inputs work but not the pepperoni?Needful
noteB works because its name is unique to two radio buttons. So one can be selected among the two. But in HTML:Radio tag the attribute's value is going to be the group name for the radio buttons. Hence when you call all the radio buttons with the same name, they'll be grouped together thus only one of them will be selected when the getter returns true. What you should do is create an actionform whose variables uniquely represent a radio button group and getters for all each group.Outspeak
An action form example. Class Aform extends ActionForm { private boolean noteB; private boolean pepperoni; //getters and setters for both} <html: radio property = "noteB" > yes <%-- so on, you get the picture --℅>Outspeak
I misunderstood your question: There are ~20 sets of two-radio groups. The radio names should be unique per group, but there are two radio inputs of the same name in each group.Needful
Yes, so if there are two radio buttons in one set, it is OK to have same name in property attribute, I think you have the same property attribute name for all the 20pairs of radio buttons making it all come under one group.Outspeak
Nope, all of the groups are named differently. Thanks so much for your help so far, I apologize for this issues frustrations. :)Needful

© 2022 - 2024 — McMap. All rights reserved.