struts2中得到checkbox的值:

来源:互联网 发布:mac book pro型号大全 编辑:程序博客网 时间:2024/05/29 12:18

struts2中得到checkbox的值:

action中得到的是一个字符串,用","隔开。所以在action中定义一个属性值接收checkbox的name,然后拆串即可。

html:

Html代码  收藏代码
  1. <input type="checkbox" name="check" value="111">  
  2. <input type="checkbox" name="check" value="222">  
  3. <input type="checkbox" name="check" value="333">  

 action:

Java代码  收藏代码
  1. private String check;  
  2. //getter and setter..  
  3. String ids = this.getCheck();  
  4. System.out.println("ids::"+ids);  
  5. String[] arraycheck = ids.split(", ");  

 这样action就得到了checkbox的value值。

注意:action为我们取到得数组格式为[val1,  val2,   val3]的形式,逗号后边带个空格,所以用split拆分字符串的时候参数要传入", "而不是",",否则在遍历该数组的时候,只有val1的值取出是正确的。




HTML code
<html> <head> <base href="<%=basePath%>"> <title>Struts2 复选框数据传值</title> <script type="text/javascript"> function checkAll(){ var listc = document.getElementsByName("listCheck"); if(document.getElementById("CheckAll").checked==true){ for(var i=0;i<listc.length;i++){ listc[i].checked=true; } }else{ for(var i=0;i<listc.length;i++){ listc[i].checked=false; } } } </script> </head> <body> <form action="test" method="post">&nbsp;&nbsp; <input type="checkbox" name="CheckAll" id="CheckAll" onclick="checkAll()"/>全选<br/> <input type="checkbox" name="listCheck" value="111" />111<br/> <input type="checkbox" name="listCheck" value="222" />222<br/> <input type="checkbox" name="listCheck" value="333" />333<br/> <input type="checkbox" name="listCheck" value="444" />444<br/> <input type="checkbox" name="listCheck" value="555"/>555 <br/> <input type="submit" value="提交"/> </form> <c:forEach items="${listCheck }" var="che"> <c:out value="${che }" /> &nbsp;&nbsp; &nbsp;&nbsp; </c:forEach> </body></html>

Java code
package com.okttl.web.action;import java.util.List;import com.okttl.entity.Users;import com.opensymphony.xwork2.ActionSupport;public class CheckBoxTestAction extends ActionSupport { private List listCheck;  public List getListCheck() {  return listCheck; } public void setListCheck(List listCheck) {  this.listCheck = listCheck; } public String findAll(){  System.out.println("我的复选框提交测试");  System.out.println(listCheck);  return SUCCESS; }}



原创粉丝点击