checkboxllist标签可以一次创建多个复选框,相当于多个<input type="checkbox"标签> ,它根据list属性指定的集合来生成多个复选框。
一个简单的index.jsp:
通过Map对象输出复选框的值,譬如:
使用集合里存放的多个javabean来生成多个复选框:
调用getRyan方法返回一个包含多个User实例的数组。
在jsp页面中使用:
一个简单的index.jsp:
<
s:form action
=
"
first
"
>
< s:checkboxlist name = " sds " list = " {'一辉','撒卡','童虎','加隆'} " label = " 请选择你所喜欢的圣斗士 " >
</ s:checkboxlist >
< s:submit value = " submit " ></ s:submit >
</ s:form >
action中:
< s:checkboxlist name = " sds " list = " {'一辉','撒卡','童虎','加隆'} " label = " 请选择你所喜欢的圣斗士 " >
</ s:checkboxlist >
< s:submit value = " submit " ></ s:submit >
</ s:form >
public
String[] getSds() {
return sds;
}
public void setSds(String[] sds) {
this .sds = sds;
}
@Override
public String execute() throws Exception {
for ( int i = 0 ; i < sds.length;i ++ ){
System.out.println(sds[i]);
}
return SUCCESS;
}
运行结果:依次打印出你所选择的checkbox所对应的值
return sds;
}
public void setSds(String[] sds) {
this .sds = sds;
}
@Override
public String execute() throws Exception {
for ( int i = 0 ; i < sds.length;i ++ ){
System.out.println(sds[i]);
}
return SUCCESS;
}
运行结果:依次打印出你所选择的checkbox所对应的值
通过Map对象输出复选框的值,譬如:
<
s:form action
=
"
ryansds
"
>
< s:checkboxlist name = " b " list = " #{'凤凰座':'一辉','双子座':'撒卡','白羊座':'史昂','天枰座':'童虎'} "
label = " 请选择你最喜欢的圣斗士 " labelposition = " top "
listKey = " value "
listValue = " key " >
</ s:checkboxlist >
< s:submit value = " submit " ></ s:submit >
</ s:form >
在ryansds对应的action中,做一个输出,看看传过来的值:
< s:checkboxlist name = " b " list = " #{'凤凰座':'一辉','双子座':'撒卡','白羊座':'史昂','天枰座':'童虎'} "
label = " 请选择你最喜欢的圣斗士 " labelposition = " top "
listKey = " value "
listValue = " key " >
</ s:checkboxlist >
< s:submit value = " submit " ></ s:submit >
</ s:form >
public
class
RyanSDS
extends
ActionSupport{
private String[] b;
public String[] getB() {
return b;
}
public void setB(String[] b) {
this .b = b;
}
@Override
public String execute() throws Exception {
for ( int i = 0 ;i < b.length;i ++ ){
System.out.println(b[i]);
}
return SUCCESS;
}
}
注意:checkboxllist标签有两个属性,listKey和listValue,他们代表了list中元素的某个属性,默认情况下 lisKey="key"(这里的key代表map中的key值,也是会传给strut2的值),listValue="value"(这里的value代表map中的value值,他会在页面中显示出来,提交表单时,并不能被struts取得)。 如果你想将map中的key值显示在页面,可以设置listKey="value."
private String[] b;
public String[] getB() {
return b;
}
public void setB(String[] b) {
this .b = b;
}
@Override
public String execute() throws Exception {
for ( int i = 0 ;i < b.length;i ++ ){
System.out.println(b[i]);
}
return SUCCESS;
}
}
使用集合里存放的多个javabean来生成多个复选框:
package
com.yan.test;
public class User {
private String username;
private String password;
public User(String username,String password) {
this .username = username;
this .password = password;
}
。。。。省略POJO
}
public class User {
private String username;
private String password;
public User(String username,String password) {
this .username = username;
this .password = password;
}
。。。。省略POJO
}
调用getRyan方法返回一个包含多个User实例的数组。
public
class
UserService {
public User[] getRyan(){
return new User[]{
new User( " ryan " , " ryanchen " ),
new User( " zhengyao " , " YOYO " ),
new User( " boys " , " girls " )
};
}
}
public User[] getRyan(){
return new User[]{
new User( " ryan " , " ryanchen " ),
new User( " zhengyao " , " YOYO " ),
new User( " boys " , " girls " )
};
}
}
在jsp页面中使用:
<
s:bean name
=
"
com.yan.test.UserService
"
id
=
"
sds
"
/>
< s:checkboxlist name = " sdfsd " list = " #sds.ryan "
listKey = " username "
listValue = " password " >
</ s:checkboxlist >
浏览该页面:
< s:checkboxlist name = " sdfsd " list = " #sds.ryan "
listKey = " username "
listValue = " password " >
</ s:checkboxlist >
浏览该页面:
-- 学海无涯