在formbean的属性最好是用String[].
FormBean:
public
class CkeckBoxForm
extends ActionForm {
private String password;
private String username;
private String[] hobby;
//接着一些Getters & Setters
private String password;
private String username;
private String[] hobby;
//接着一些Getters & Setters
JSP:
<
html:form
action
="/checkBox"
>
Username : < html:text property ="username" /> < br />
password : < html:password property ="password" /> < br />
Hobby: < br />
< html:checkbox property ="hobby" value ="Java" >Java </html:checkbox>
< html:checkbox property ="hobby" value ="VC" >VC </html:checkbox>
< html:checkbox property ="hobby" value ="VB" >VB </html:checkbox>
< html:submit /> < html:cancel />
</html:form>
Username : < html:text property ="username" /> < br />
password : < html:password property ="password" /> < br />
Hobby: < br />
< html:checkbox property ="hobby" value ="Java" >Java </html:checkbox>
< html:checkbox property ="hobby" value ="VC" >VC </html:checkbox>
< html:checkbox property ="hobby" value ="VB" >VB </html:checkbox>
< html:submit /> < html:cancel />
</html:form>
Action:
public ActionForward execute(mapping,form,request,response){
CheckBoxForm cbf = (CheckBoxForm) form;
String[] t = cbf.getHobby();
for (int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
CheckBoxForm cbf = (CheckBoxForm) form;
String[] t = cbf.getHobby();
for (int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
.....
}
这样,我就可以知道用户选择的是哪些checkbox 了
本文转自NightWolves 51CTO博客,原文链接:
http://blog.51cto.com/yangfei520/234467
,如需转载请自行联系原作者