获取弹簧控制器中的复选框值

时间:2013-01-15 10:21:42

标签: spring-mvc

enter image description here

我希望spring控制器中的多个标记复选框值在spring中执行多个删除任务,如何获取标记复选框的这些值,如图所示

这里是显示部门详细信息列表的jsp页面,我已经给出了删除多个部门的复选框选项 这是我的jsp页面

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
    function setAction(url) {

        if (url == 'FindByDepartmentID') {
            document.form.action = "/EmployeeWebSpring/department/FindByDepartmentId";

        }
        if (url == 'FindByDepartmentName') {
            document.form.action = "/EmployeeWebSpring/department/FindByDepartmentName";
        }

    }
    function showMe(it) {

        var hasChecked = false;
        var chks = document.getElementsByName('checked');
        var vis = '';

        for ( var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                hasChecked = true;
                break;
            }
        }

        if (hasChecked == false) {
            vis = "none";
        } else {
            vis = "block";
        }

        document.getElementById(it).style.display = vis;

    }
</script>
</head>
<body>
    <form:form name="form" commandName="departmentForm" method="post">
        <c:if test="${requestScope.x}">
            <div id="div1">
                <center>
                    Department_ID:<br />
                    <form:errors path="departmentId" />
                    <br /> <br />
                    <form:input path="departmentId" />
                    <br /> <input type="submit" name="FindByDepartmentID"
                        value="FindByDepartmentID" id="FindByDepartmentID"
                        onclick="setAction(this.id)" />
                </center>
            </div>
        </c:if>
        <c:if test="${requestScope.y}">
            <div id="div2">

                Department_Name
                <form:input path="departmentName" />
                <input type="submit" name="method" value="FindByDepartmentName"
                    id="FindByDepartmentName" onclick="setAction(this.id)" /> <br />
                <font size=3>For Searching the employees by<b>Employee
                        Name</b><br />you can use % match all the records with the given
                    pattern
                </font><br /> <font size="2"> <i>e.g <b> for search by</b>EmployeeName<br />
                        matches alL the employees whose name starts with character <b>S</b></i></font>
            </div>
        </c:if>

        <c:if test="${departmentName}">
            <div>
                <fmt:message key="searchResult.header" />
                <br>
                <c:choose>
                    <c:when test="${empty requestScope.listDepartmentBO}">
                        <fmt:message key="searchResult.noresult" />
                    </c:when>
                    <c:otherwise>
                        <table border="2">
                            <tr>
                                <th width="118">Delete</th>
                                <th width="118">Department_ID</th>
                                <th width="118">Department_Name</th>
                                <th width="118">Department_Location</th>
                                <th width="118">detail_Link</th>
                            </tr>
                            <c:forEach var="departmentBO"
                                items="${requestScope.listDepartmentBO}">
                                <tr>
                                    <td><form:checkbox path="departmentId"
                                            value='${departmentBO.departmentId}' onclick="showMe('div1')" id="checked"/></td>

                                    <td>${departmentBO.departmentId}</td>

                                    <td>${departmentBO.departmentName}</td>

                                    <td>${departmentBO.departmentLocation}</td>
                                    <td><a
                                        href="javascript:loadId(${departmentBO.departmentId})"><font
                                            color="blue">Details</font></a></td>


                                </tr>
                            </c:forEach>
                        </table>
                    </c:otherwise>
                </c:choose>

                <br />
                <fmt:message key="searchResult.searchAgain" />
                <a href="/EmployeeWebSpring/search/searchspring"> <fmt:message
                        key="searchResult.click" />
                </a>

            </div>
        </c:if>
        <div id="div1"
            style="position: absolute; width: 600px; display: none;">
            <input type="submit" name="create" value="delete" align="middle"
                style="size: 100px" />
        </div>
    </form:form>
</body>
</html>

并且这是我的控制器删除功能,现在它正在执行单个删除任务

package com.nousinfo.tutorial.controllers;  import java.util.Map;

import javax.validation.Valid;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nousinfo.tutorial.model.DepartmentForm;
import com.nousinfo.tutorial.service.impl.DepartmentServiceImpl;
import com.nousinfo.tutorial.service.model.DepartmentBO;

@RequestMapping("departmentController")
public class DepartmentController {
    private DepartmentServiceImpl departmentServiceImpl;

    public DepartmentServiceImpl getDepartmentServiceImpl() {
        return departmentServiceImpl;
    }

    public void setDepartmentServiceImpl(
            DepartmentServiceImpl departmentServiceImpl) {
        this.departmentServiceImpl = departmentServiceImpl;
    }

    @RequestMapping(value = "/departmentForm", method = RequestMethod.GET)
    public String view(Model model) throws Exception {
        DepartmentForm departmentForm = new DepartmentForm();
        model.addAttribute("departmentForm", departmentForm);
        return "departmentForm";
    }

    @RequestMapping(value = "/createDepartment", method = RequestMethod.POST)
    public String createEmployee(@Valid DepartmentForm departmentForm,
            BindingResult bindingResult, Map<String, DepartmentForm> model)
            throws Exception {
        String form = null;
        if (bindingResult.hasErrors()) {
            return "departmentForm";
        }

        model.put("departmentForm", departmentForm);
        departmentForm.setUpdateStatus("A");
        if (departmentForm.getUpdateStatus().charAt(0) == 'A') {
            boolean flag = departmentServiceImpl
                    .actionDecider(convertDeptFormToDeptBO(departmentForm));
            if (flag == false)
                form = "DBError";
            else
                form = "Success";

        }
        return form;
    }


    @RequestMapping(value = "/deleteDepartment", method = RequestMethod.POST)
    public String deleteDepartment(
            @ModelAttribute("departmentForm") DepartmentForm departmentForm) {
        String form = null;
        departmentForm.setUpdateStatus("D");
        if (departmentForm.getUpdateStatus().charAt(0) == 'D') {
            boolean flag = departmentServiceImpl
                    .actionDecider(convertDeptFormToDeptBO(departmentForm));
            if (flag == false)
                form = "DBError";
            else
                form = "Success";

        }
        return form;
    }

    private DepartmentBO convertDeptFormToDeptBO(DepartmentForm departmentForm) {
        DepartmentBO departmentBO = new DepartmentBO();
        departmentBO
                .setUpdateStatus(departmentForm.getUpdateStatus().charAt(0));
        departmentBO.setDepartmentId(departmentForm.getDepartmentId());
        departmentBO.setDepartmentName(departmentForm.getDepartmentName());
        departmentBO.setDepartmentLocation(departmentForm
                .getDepartmentLocation());

        return departmentBO;

    }

}

1 个答案:

答案 0 :(得分:1)

您可以在jquery循环中构建部门ID列表:

    for ( var i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            list.add(getDepartmentId());
        }

然后通过ajax调用将其传递给您的控制器方法:

$.ajax({
  url: deleteDepartment,
  type: 'POST',
  data: list
  })
  .done(function() {
    console.log("success");
  }); 

在您的控制器方法中,将ID列表包含为requestParam:

deleteDepartment(@RequestParam List<String> list, @ModelAttribute("departmentForm") DepartmentForm departmentForm)

并在您的控制器方法内部执行查询以删除部门ID在您传入的列表中的所有记录。