Spring mvc 在spring控制器中获取复选框值
您好,我希望spring控制器中的多个标记复选框值在spring中执行多个删除任务,如何获取标记复选框的这些值,如图所示 这里是jsp页面,它显示了部门详细信息列表,我已经给出了删除多个部门的复选框选项 这是我的jsp页面Spring mvc 在spring控制器中获取复选框值,spring-mvc,Spring Mvc,您好,我希望spring控制器中的多个标记复选框值在spring中执行多个删除任务,如何获取标记复选框的这些值,如图所示 这里是jsp页面,它显示了部门详细信息列表,我已经给出了删除多个部门的复选框选项 这是我的jsp页面 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://jav
<%@ 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>
在此处插入标题
函数setAction(url){
如果(url='FindByDepartmentID'){
document.form.action=“/EmployeeWebSpring/department/FindByDepartmentId”;
}
如果(url='FindByDepartmentName'){
document.form.action=“/EmployeeWebSpring/department/FindByDepartmentName”;
}
}
函数showMe(it){
var hasChecked=false;
var chks=document.getElementsByName('checked');
var-vis='';
对于(变量i=0;i
部门名称
用于按员工搜索员工
Name
您可以使用%match将所有记录与给定的
图案
例如按员工姓名搜索
匹配名称以字符S开头的所有员工
删除
部门ID
部门名称
部门地点
详细信息链接
${departmentBO.departmentId}
${departmentBO.departmentName}
${departmentBO.departmentLocation}
这是我的控制器删除功能,它现在执行单个删除任务
包com.nousinfo.tutorial.controllers;
导入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;
}
}
import javax.validation.Valid;
导入org.springframework.ui.Model;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入com.nousinfo.tutorial.model.DepartmentForm;
导入com.nousinfo.tutorial.service.impl.DepartmentServiceImpl;
导入com.nousinfo.tutorial.service.model.DepartmentBO;
@请求映射(“部门控制器”)
公共课系主任{
私人部门服务impl部门服务impl;
公共部门服务impl getDepartmentServiceImpl(){
返回部门ServiceImpl;
}
公共无效setDepartmentServiceImpl(
DepartmentServiceImpl DepartmentServiceImpl){
this.departmentServiceImpl=departmentServiceImpl;
}
@RequestMapping(value=“/departmentForm”,method=RequestMethod.GET)
公共字符串视图(模型)引发异常{
DepartmentForm DepartmentForm=新的DepartmentForm();
model.addAttribute(“departmentForm”,departmentForm);
返回“部门表格”;
}
@RequestMapping(value=“/createDepartment”,method=RequestMethod.POST)
公共字符串createEmployee(@Valid DepartmentForm DepartmentForm,
BindingResult(绑定结果,映射模型)
抛出异常{
字符串形式=空;
if(bindingResult.hasErrors()){
返回“部门表格”;
}
模型.put(“部门表格”,部门表格);
部门表格setUpdateStatus(“A”);
if(departmentForm.getUpdateStatus().charAt(0)='A'){
布尔标志=departmentServiceImpl
.actionDecider(convertDeptFormToDeptBO(部门表格));
如果(标志==false)
form=“DBError”;
其他的
form=“成功”;
}
申报表;
}
@RequestMapping(value=“/deleteDepartment”,method=RequestMethod.POST)
公共部门(
@ModelAttribute(“部门表单”)部门表单(部门表单){
字符串形式=空;
部门表格setUpdateStatus(“D”);
if(departmentForm.getUpdateStatus().charAt(0)='D'){
布尔标志=departmentServiceImpl
.actionDecider(convertDeptFormToDeptBO(部门表格));
如果(标志==false)
form=“DBError”;
其他的
form=“成功”;
}
申报表;
}
私人部门BO convertDeptFormToDeptBO(部门表格部门表格){
DepartmentBO DepartmentBO=新的DepartmentBO();
办公室
.setUpdateStatus(departmentForm.getUpdateStatus().charAt(0));
departmentBO.setDepartmentId(departmentForm.getDepartmentId
for ( var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
list.add(getDepartmentId());
}
$.ajax({
url: deleteDepartment,
type: 'POST',
data: list
})
.done(function() {
console.log("success");
});
deleteDepartment(@RequestParam List<String> list, @ModelAttribute("departmentForm") DepartmentForm departmentForm)