Back Forum Reply New

enusuring lt;form:radiobutton/gt; is selected

It seems like this is a pretty straightforward scenario. We have a variable that holds either the value of quot;1quot; or quot;0.quot; If the user selects one radio button, we want to bind quot;1quot; to that variable, and if they choose the other radio button, we want to bind quot;0quot; to it. So, we have something like this:Code:
lt;form:radiobutton path=quot;myPath.valuequot; value=quot;1quot; /gt;amp;On
lt;form:radiobutton path=quot;myPath.valuequot; value=quot;0quot; /gt;amp;Off
Now, it binds to the variable without a problem. The problem is when we want it to do prepopulation. If the user selects the first radio button, everything works perfectly. The value of quot;1quot; is bound, and when the page reloads, the first radio button is selected. However, if the user selects the 2nd radio button, it doesn't work like we want. The value of quot;0quot; is bound, but when the page comes back, neither radio button is selected. I assume it's because the value is quot;0?quot; I changed the values to quot;1quot; and quot;2,quot; and it works fine. We have our server side logic already assuming quot;1quot; and quot;0quot; for the values. I really don't want to have to change it. So, is there any way to force a radio button to be selected, even if the value is quot;0?quot; Any help would be much appreciated. Thanks!

My first thought is to change your values so that 0 is not an option (i.e. use 1 and 2 for values not 0 and 1), is that even an option? Anyone else have any ideas on how to do this. I am not very familliar with the way radio buttons work.

We really don't want to have to change the values we are using for the radio buttons. Does anyone know how we can do this and keep the values of 0 and 1?

Hi dnc253,

I did it in this way.
Code:
public class MyPath {

private int value;
private boolean selected;
private String desc;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Command Bean class

Code:public class CreateClient {

private String fullname;
private String userid;
private MyPath myPath;

public void setFullname(String s) {
fullname = s;
}

public void setUserid(String s) {
userid = s;
}public void setMyPath(MyPath s) {
myPath = s;
}
// /////////////////////////////
public String getFullname() {
return fullname;
}

public String getUserid() {
return userid;
}
public MyPath getMyPath() {
return myPath;
}

}
formBacking method in your controller

Code:
protected Object formBackingObject(fromServletRequest request)
throws ServletException {    CreateClient createClient = new CreateClient();
createClient.setFullname(quot;quot;);
createClient.setUserid(quot;quot;);   MyPath myPath = new MyPath();
myPath.setDesc(quot;testquot;);
myPath.setValue(0);/* If you set this as 0 then radio button with value 0 will be selected amp; If this value is 1 then radio button with value 1 willbe selected */
myPath.setSelected(true);
createClient.setMyPath(myPath);return createClient;

}
In the JSP,

Code:
lt;form:radiobutton path=quot;myPath.valuequot; value=quot;1quot; /gt;amp;On
lt;form:radiobutton path=quot;myPath.valuequot; value=quot;0quot; /gt;amp;Off

If you need to set the radio button related to quot;Onquot; , Then set the value property of your myPath variable to 1 amp; Vice versa.thx amp; rgrds,

Manjula

I think what you need is not my above answer.
Sorry I have not read your post properly.
But I think You should set the selected radio button's value ( get it from request object or command object) again as your myPath variables value in formBacking method.
I'll try to find how to do that.

thx amp; rgrds,
Manjula

Ok, we figured it out. The problem was on our end. In the midst of all our data processing, we were actually sending quot;quot; to Spring and not quot;0.quot; We have some code that changes quot;0quot; to quot;quot; in certain cases, and that was what was happening here. We changed the check to allow this scenario, and now it works.
¥
Back Forum Reply New