天天看点

struts2中得到checkbox的值:

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
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
     >
     
  
     <
     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"
     >
       
     
     
     <
     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 }"
      
     />
      
       
      
       
     
    
     </
     c:forEach
     >
     
  
     </
     body
     >
     

     </
     html
     >      
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;
 }
}