【问题标题】:How to pass checkbox values to the controller in Spring MVC如何在 Spring MVC 中将复选框值传递给控制器
【发布时间】:2015-10-15 15:07:52
【问题描述】:

我有一个带有函数列表的 jsp 页面。在控制器中,我从数据库中获取此列表并将其传递给 jsp。

@RequestMapping(value = "/functionlist", method = RequestMethod.GET)
    public ModelAndView functionList(Model model) throws Exception {
        ModelAndView mv = new ModelAndView("functionList");
        mv.addObject("functionList", getFunctionsFromDB());
        return mv;
    }

在我的 jsp 页面中,我使用此函数列表创建表

<table id="table">
    <thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>
    <c:choose>
        <c:when test="${not empty functionList}">
            <c:forEach var="function" items="${functionList}">
                <tr>
                    <td><input name="id" value="${function.id}" hidden></td>
                    <td>${function.name}</td>
                    <td>
                        <input type="checkbox" id="${function.id}" value="${function.action}"></td>
                </tr>
            </c:forEach>
        </c:when>
    </c:choose>
    </tbody>
</table>
<button type="submit" name="save">Save</button>

我还将函数 id 赋予复选框 id。

我的 Function 实体如下

public class Function {

    private Integer id;
    private String name;
    private Boolean action;
...
}

我想按下按钮保存并进入控制器“/functionlist/save”我的复选框值列表。

【问题讨论】:

标签: spring jsp spring-mvc checkbox


【解决方案1】:

尝试像这样将form添加到你的jsp页面

  <form:form id="yourForm" action="/functionlist/save" method="POST" modelAttribute="functionList">

        <c:forEach items="${functionList}" varStatus="status" var="function">

            <tr>
                <td>${function.name}</td>
                <td>
                    <form:checkbox path="functionList[${status.index}].action"/>
                </td>
            </tr>
        </c:forEach>

        <input type="submit" value="submit" />
    </form:form>

在Controller中你应该有这样的方法

  @RequestMapping(value = { "/functionlist/save" }, method = RequestMethod.POST)
    public String savePerson(@ModelAttribute("functionList")List<Function> functionList) {
        // process your list
    }

如果这不起作用,您可以尝试包装您的列表。

public class FunctionListWrapper {
    private List<Function> functionList;

    public FunctionListWrapper() {
        this.functionList = new ArrayList<Function>();
    }

    public List<Function> getFunctionList() {
        return functionList;
    }

    public void setFunctionList(List<Function> functionList) {
        this.functionList = functionList;
    }

    public void add(Function function) {
        this.functionList.add(function);
    }
}

在控制器中,而不是传递列表,传递包装器

  FunctionListWrapper functionListWrapper=new FunctionListWrapper();
        functionListWrapper.setFunctionList(userService.getFunctionList());
        mv.addObject("functionListWrapper", functionListWrapper);

更多详情请查看以下问题:question 1question 2

【讨论】:

    【解决方案2】:

    首先,您需要在复选框输入中添加名称属性。您可以在 控制器上与您的名称属性同名的数组。例如-

        @RequestMapping(value = "/functionlist", method = RequestMethod.GET)
            public ModelAndView functionList(Model model,@RequestParam("checkboxname")String[] checkboxvalues) throws Exception {
            ModelAndView mv = new ModelAndView("functionList");
            mv.addObject("functionList", getFunctionsFromDB());
           return mv;
    }
    

    【讨论】:

      【解决方案3】:

      几件事。 首先你应该考虑一个表单对象,它是视图和控制器之间的交换实体。表单实体将类似于以下内容:

      public class Form {
      private List<Function> functions;
      //getters & setters
      }
      

      其次,由于您使用的是 Spring MVC,因此您应该利用 &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt; 库。因此,您的表单将是:

      <form:form  commandName="form"  action="/your/url">
      <c:forEach items="${form.functions }" var="function" varStatus="status">
          <tr>
              <td><form:hidden path="functions[${status.index }].id"></td>
              <td>${function.name}</td>
              <td>
                  <form:checkbox path="functions[${status.index }].action" id="${function.id}" value="${function.action}"/>
              </td>
          </tr>
      </c:forEach>
      </form:form>
      

      最后控制器会是这样的

      @RequestMapping(value="/your/url", method=RequestMethod.POST)
      public String postForm(@ModelAttribute("form") FormForm form)
      {
          //cool stuff here
      }
      

      请注意,正确的 HTTP 方法是 POST,而不是 GET。是的,我知道 GET 也适用,这绝对是一种已弃用的策略。 另外,弄清楚我是如何在Form 中引用Function 的列表的。当我必须显示数据时,我在foreach 语句中使用了变量function,当我必须绑定Form 对象以将数据传输到控制器时,我引用了该列表。最后但并非最不重要的一点是,我还没有尝试过代码。我写它只是为了给你关于如何正确地做的提示。 最后一件事。由于您将表单传递给 JSP,因此您不再需要使用您使用的属性填充模型。只需在表格中填写您的数据并使用它在视图中显示它们。就像这样${form.stuff.you.want}

      【讨论】:

        猜你喜欢
        • 2012-12-31
        • 1970-01-01
        • 2019-08-09
        • 2018-12-15
        • 1970-01-01
        • 2011-09-25
        • 2016-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多