<s:checkbox name="bz" fieldValue="Y" /></td>选中checkbox后,数据库中存Y,不选中存N,已经实现,但希望效果:刷新后可以看到选中的状态:
提交后不要直接返回jsp,先调用查询方法,查询方法返回jsp。查询方法中把db里的状态查出来,在jsp里逻辑判断如果是y就把checkbox设为checked
又看了一下你的问题,你这个一开始就该用radio,checkbox是复选框,是可以多选的,你这种情况应该用radio单选框,如果你一定要用checkbox获得服务器返回的选项,Struts2中的checkbox不提供这个功能,而是靠另一个标签checkboxlist实现的
<body>
<s:form action="testAction!execute">
<s:checkbox name="ishere" fieldValue="true" label="在职情况"> </s:checkbox>
<s:submit name="" value="提交"> </s:submit>
</s:form>
</body>
在action中,我用一个Boolean类型的ishere接收
private Boolean ishere;
public String execute() throws Exception{
System.out.println("ishere长度"+ishere);
return null;
}
public Boolean getIshere() {
return ishere;
}
public void setIshere(Boolean ishere) {
this.ishere = ishere;
}
如果我将checkbox选中,则在action中打印true,不选中则打印false。这很正确
现在我要将页面和action都该一下就不行了。修改如下:
<s:form action="testAction!execute">
<s:checkbox name="ishere" fieldValue="true" label="在职情况"> </s:checkbox>
<s:checkbox name="ishere" fieldValue="true" label="在职情况"> </s:checkbox>
<s:checkbox name="ishere" fieldValue="true" label="在职情况"> </s:checkbox>
<s:checkbox name="ishere" fieldValue="true" label="在职情况"> </s:checkbox>
<s:submit name="" value="提交"> </s:submit>
</s:form>我多加了几个checkbox,并且命名相同
然后在action中用数组接收
private Boolean[] ishere;
public String execute() throws Exception{
System.out.println("ishere长度"+ishere);
for(int i=0;i <ishere.length;i++){
System.out.println("-----------"+ishere[i]);
}
return null;
}
public Boolean[] getIshere() {
return ishere;
}
public void setIshere(Boolean[] ishere) {
this.ishere = ishere;
}
如果我只选择两个checkbox就提交,则ishere数组的长度也只有2,打印的值都是true。我不懂的是为什么struts的默认机制没有将我没有选中的两个checkbox的false值存到数组中呢?有什么办法能解决吗?