每当我选择多个复选框保存在数据库中时,它就会显示错误,而当我没有选择任何复选框时,只要写出组名,它就会将名称保存在数据库中。
GroupDetail.java
package com.userauthenticate.model;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class GroupDetail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String groupName;
@ManyToMany
private Set<UserDetail> user= new HashSet<UserDetail>();
@OneToMany
private Set<Role> role= new HashSet<Role>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public Set<UserDetail> getUser() {
return user;
}
public void setUser(Set<UserDetail> user) {
this.user = user;
}
public Set<Role> getRole() {
return role;
}
public void setRole(Set<Role> role) {
this.role = role;
}
}
GroupController.java
package com.userauthenticate.controller;
import com.userauthenticate.model.GroupDetail;
import com.userauthenticate.service.GroupDetailService;
import com.userauthenticate.service.RoleService;
import com.userauthenticate.service.UserDetailService;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class GroupController {
@Autowired
private GroupDetailService groupDetailService;
@Autowired
private RoleService roleService;
@Autowired
private UserDetailService userDetailService;
private String msg = "namme in group";
@RequestMapping("grouplist")
public ModelAndView grouplist() {
ModelAndView modelAndView = new ModelAndView("grouplists");
modelAndView.addObject("message", msg);
modelAndView.addObject("grouplist", groupDetailService.getGrouplist());
modelAndView.addObject("rolelist", roleService.getRolelist());
modelAndView.addObject("userlist", userDetailService.getUserList());
modelAndView.addObject("group", new GroupDetail());
return modelAndView;
}
@RequestMapping(value = "/addgroup", method = RequestMethod.POST)
public ModelAndView addgroup(@ModelAttribute("group") GroupDetail group) {
// System.out.println(333);
System.out.println(group.getGroupName());
groupDetailService.add(group);
return new ModelAndView("redirect:/grouplist");
}
}
Grouplists.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form"
uri="http://www.springframework.org/tags/form" %>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1" />
<title>Home</title>
</head>
<body>
<h1>Welcome, ${message}</h1> <a href="/">LogOut</a>
<table>
<tbody>
<form:form action="/addgroup" method="post" modelAttribute="group">
<tr>
<td>Group Name</td>
<td><form:input type="text" name="eee" path="groupName"/></td>
</tr>
<tr>
<td>Role</td>
<td>
<%--<form:checkboxes path="role" items="${rolelist}" itemValue="${rolelist.id}"/>--%>
<c:forEach items="${rolelist}" var="r">
<form:checkbox value="${r.id}" path="role"/> ${r.role}
</c:forEach>
</td>
</tr>
<tr>
<td>Users</td>
<td>
<c:forEach items="${userlist}" var="u">
<form:checkbox value="${u.email}" path="user"/> ${u.name}(${u.email})
</c:forEach>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Add"/>
</td>
</tr>
</form:form>
</tbody>
</table>
</body>
</html>
错误:
400-错误的请求服务器无法或将不处理请求,原因是 认为是客户错误的内容(例如格式错误) 请求语法,无效的请求消息框架或欺骗性请求 路由)。
答案 0 :(得分:0)
您勾选的复选框将获取值并将其放入值数组中。
@RequestMapping(value = "/addgroup",method = RequestMethod.POST)
public ModelAndView addgroup(@RequestParam("role") int[] roleid, @RequestParam("groupName") String groupname, @RequestParam("user") String[] email){
if (!groupname.equals(""))
groupDetailService.add(roleid,groupname,email);
return new ModelAndView("redirect:/grouplist");
}