2
我试图通过绑定Spring中的student
对象来从JSP页面获取courseList
。以下是我的Student
班。但是,即使我选中了复选框,ArrayList
的大小也是0.请问您为什么会这样?问题在弹簧中使用复选框绑定类别中的ArrayList
谢谢。
P.S. 这是我收到的错误:
[Field error in object 'student' on field 'courseList': rejected value
[[email protected],[email protected]];
codes [typeMismatch.student.courseList,typeMismatch.courseList,typeMismatch.java.util.List,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.courseList,courseList]; arguments []; default message [courseList]];
default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'courseList'
这是学生类的一部分。它正确地包含了所有的getter和setter。
@Table(name="student")
public class Student {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
private String grade;
@Column(name="home_number")
private String homeNumber;
@Column(name="home_address")
@Lob
private String homeAddress;
@Column(name="date_of_registration")
@Temporal(TemporalType.DATE)
private Date dateOfRegistration;
@ManyToMany
@JoinTable(name="course_student",[email protected](name="student_id"),[email protected](name="course_id"))
private List<Course> courseList = new ArrayList<Course>();
这是我的jsp页面代码。
<%@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/core" prefix="c"%>
<!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=UTF-8">
<title>Food List</title>
</head>
<body>
${message}
<br /> ${operation}
<br /> Please Select Subjects for the Student ${student.firstName}
<br /> Student Id is ${student.id}
<br />
<form:form method="post" action="${action}" modelAttribute="student">
<table>
<tr>
<th>enroll</th>
<th>Subject</th>
</tr>
<form:hidden path="id" value="${student.id}"/>
<c:forEach items="${avacourses}" var="course" varStatus="status">
<tr>
<td><form:checkbox path="courseList" value="${course}" /></td>
<td>${course.courseName}</td>
</tr>
</c:forEach>
</table>
<br />
<input type="submit" value="enroll" />
</form:form>
</body>
</html>
下面是我处理它的控制器代码。
@RequestMapping("/addstudent")
public ModelAndView addStudent(@ModelAttribute("student")Student student,BindingResult result){
student = studentService.addStudent(student);
Collection<Course> availableCourses = courseService.getAvailableCourses(student.getGrade());
ModelAndView mav = new ModelAndView();
mav.setViewName("/course/courselist");
mav.addObject("operation", "Enroll the courses to the student");
mav.addObject("action", "enroll.html");
mav.addObject("student", student);
mav.addObject("avacourses", availableCourses);
return mav;
}
@RequestMapping("/enroll")
public ModelAndView ModelAndView(@ModelAttribute("student")Student student, BindingResult result){
System.out.println(student.getId());
System.out.println(student.getCourseList().size());
ModelAndView mav = new ModelAndView("/student/student","command",new Student());
mav.addObject("operation", "Add a new Student");
return mav;
}
你应该检查绑定错误'if(result.hasErrors()){//记录它} – nickdos