How to retrieve checkbox values in Struts 2 action class ?

后端 未结 2 728
日久生厌
日久生厌 2021-01-07 06:29

I have dynamic number of checkboxes in jsp page as given below.


         

        
2条回答
  •  误落风尘
    2021-01-07 07:18

    Because your checkboxes are all named the same (checkbox), Struts2 is just passing the following:

    checkbox=true&checkbox=true&checkbox=true

    Does that mean that you omitted the second, third, or fourth checkbox?

    What you actually want is not an array of booleans, but a map of Integer to Boolean. Here's an example:

    Example Action

    public class MyAction extends ActionSupport {
      private Map checkboxes;
    
      ...
    
      public Map getCheckboxes() {
        return checkboxes;
      }
    
      public void setCheckboxes(Map checkboxes) {
        this.checkboxes = checkboxes;
      }
    }
    

    Example JSP

    
      <%-- this outputs checkboxes[0], checkboxes[1], etc. --%>
      
    
    

    Example Result

    • 0 -> true
    • 1 -> false
    • 2 -> true
    • 3 -> true

提交回复
热议问题