s:checkbox

 <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值存到数组中呢?有什么办法能解决吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值