How to retrieve checkbox values in Struts 2 action class ?

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

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


         

        
相关标签:
2条回答
  • 2021-01-07 07:14

    Actually I use Struts 2 3.8 and WebSphere 7.0 and Map works for rendering the check boxes, but not working when these check boxes post back. The Boolean class is rendered correctly, but during the post back in Struts 2, it becomes ognl.NoConversionPossible and ends up with No result defined for action CLASS ...and result input error.
    I replaced Boolean with String class, i.e.

    maps.put(new Integer(0), new String("true")); 
    

    and it works for me.

    0 讨论(0)
  • 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<Integer, Boolean> checkboxes;
    
      ...
    
      public Map<Integer, Boolean> getCheckboxes() {
        return checkboxes;
      }
    
      public void setCheckboxes(Map<Integer, Boolean> checkboxes) {
        this.checkboxes = checkboxes;
      }
    }
    

    Example JSP

    <s:iterator value="details" status="stat">
      <%-- this outputs checkboxes[0], checkboxes[1], etc. --%>
      <s:checkbox name="checkboxes[%{#stat.index}]"  theme="simple" />
    </s:iterator>
    

    Example Result

    • 0 -> true
    • 1 -> false
    • 2 -> true
    • 3 -> true
    0 讨论(0)
提交回复
热议问题