如何在Spring MVC中将复选框值传递给控制器

编程入门 行业动态 更新时间:2024-10-27 18:22:36
本文介绍了如何在Spring MVC中将复选框值传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含功能列表的jsp页面.在控制器中,我从数据库中获取此列表,并将其传递给jsp.

I have a jsp page with list of functions. Here in controller I get this list from database and pass it to 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页面中,我使用此功能列表创建表

In my jsp page I create table using this list of functions

<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.

I also give function id to checkbox id.

我的Function实体如下

My Function entity is the following

public class Function { private Integer id; private String name; private Boolean action; ... }

我想按保存"按钮并进入控制器"/functionlist/save"我的复选框值列表.

I want to press button Save and get in controller "/functionlist/save" my list of checkbox values.

推荐答案

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

Try to add form like this to your jsp page

<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中,您应该有这样的方法

and in Controller you should have a method like this

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

如果这不起作用,则可以尝试包装列表.

If this does not work, you can try to wrap you 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); } }

在控制器中,而不是通过列表,而是通过包装器

in controller instead of passing list, pass wrapper

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

有关更多详细信息,请查看以下问题:问题1 和问题2

For more details please take a look at this questions: question 1 and question 2

更多推荐

如何在Spring MVC中将复选框值传递给控制器

本文发布于:2023-10-12 01:39:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   控制器   复选框   如何在   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!