最近在做struts的时候用到了,html:radio来表示性别。但是在使用的过程中遇到了不能动态选中的问题。后来在网上查找后基本的解决方案是:
Each radio button element requires an assigned value that distinguishes it from the other radio buttons. When creating a static array of radio buttons, you need to indicate which one of these, if any, is checked. This does not need to be done when the radio buttons are being populated from dynamic data. The control can compare itself to the form bean's property and then check itself when appropriate.
Given a set of < html:radio > controls like this:
< html:radio property ="expectedVia" value ="UPS" /> UPS
< html:radio property ="expectedVia" value ="FEDX" /> Federal Express
< html:radio property ="expectedVia" value ="AIRB" /> AirBorne
And that the expectedVia property on the form bean was already set to "UPS", then the HTML radio elements would be rendered like this:
< input type ="radio" name ="expectedVia" value ="UPS" checked ="checked" > UPS
< input type ="radio" name ="expectedVia" value ="FEDX" > Federal Express
< input type ="radio" name ="expectedVia" value ="AIRB" > AirBorne
If you need to create a dynamic set of radio buttons, or want to localize the values, you can create in an Action a collection of LabelValueBeans with the appropriate labels and values for each button. Here's an example:
ArrayList shippers = new ArrayList();
shippers.add(new LabelValueBean("UPS", "UPS"));
shippers.add(new LabelValueBean("Federal Express", "FEDX"));
shippers.add(new LabelValueBean("AirBorne", "AIRB"));
request.setAttribute ("SHIPPERS",shippers);
Then, on the page, you can iterate through the collection
< logic:iterate id ="row" name ="SHIPPERS" type ="org.apache.commons.scaffold.util.LabelValueBean" >
< html:radio property ="expectedVia" value ="<%=row.getValue()%>" />
< bean:write name ="row" property ="label" />
</ logic:iterate >
So long as one of the values matches the "expectedVia" property on our ActionForm, the radio tag will still automatically select the appropriate button.
Given a set of < html:radio > controls like this:
< html:radio property ="expectedVia" value ="UPS" /> UPS
< html:radio property ="expectedVia" value ="FEDX" /> Federal Express
< html:radio property ="expectedVia" value ="AIRB" /> AirBorne
And that the expectedVia property on the form bean was already set to "UPS", then the HTML radio elements would be rendered like this:
< input type ="radio" name ="expectedVia" value ="UPS" checked ="checked" > UPS
< input type ="radio" name ="expectedVia" value ="FEDX" > Federal Express
< input type ="radio" name ="expectedVia" value ="AIRB" > AirBorne
If you need to create a dynamic set of radio buttons, or want to localize the values, you can create in an Action a collection of LabelValueBeans with the appropriate labels and values for each button. Here's an example:
ArrayList shippers = new ArrayList();
shippers.add(new LabelValueBean("UPS", "UPS"));
shippers.add(new LabelValueBean("Federal Express", "FEDX"));
shippers.add(new LabelValueBean("AirBorne", "AIRB"));
request.setAttribute ("SHIPPERS",shippers);
Then, on the page, you can iterate through the collection
< logic:iterate id ="row" name ="SHIPPERS" type ="org.apache.commons.scaffold.util.LabelValueBean" >
< html:radio property ="expectedVia" value ="<%=row.getValue()%>" />
< bean:write name ="row" property ="label" />
</ logic:iterate >
So long as one of the values matches the "expectedVia" property on our ActionForm, the radio tag will still automatically select the appropriate button.
上面的解释可以大概概括如下:
1.如果是固定的值,比如性别:男,女。你只需把value的属性设置为各自的值。然后当页面显示的时候会自动把从frombean中对应的数据给选中。
2.如果你是动态生成的radio,就用logic:iterate就可以了。
但是我按上述的第一中方法编写如下jsp代码:
<
html:radio
value
="M"
property
="sex"
name
="member"
>
男
</
html:radio
>
< html:radio property ="sex" value ="F" name ="member" > 女 </ html:radio >
< html:radio property ="sex" value ="F" name ="member" > 女 </ html:radio >
action过来的form bean中也设置了sex的值。但是radio就是不选中。
后来没办法,只能用java script来实现了。
<
script type
=
"
text/javascript
"
>
function setChecked(value) ... {
if(value=="M")
...{
document.forms[0].sex[0].checked=true;
}
else
...{
document.forms[0].sex[1].checked=true;
}
</script>其中memberInfo是该页面中引用form bean的<bean:define的Id。
function setChecked(value) ... {
if(value=="M")
...{
document.forms[0].sex[0].checked=true;
}
else
...{
document.forms[0].sex[1].checked=true;
}
</script>其中memberInfo是该页面中引用form bean的<bean:define的Id。
然后在jsp最后加上下面的代码:
<
script type
=
"
text/javascript
"
>
setChecked( <%= memberInfo.getSex() %> )
</ script >
setChecked( <%= memberInfo.getSex() %> )
</ script >
这样设置后运行正常!!