我试图通过绑定Spring中的courseList
对象从JSP页面获取student
。以下是我的Student
课程。但是,即使我选中了复选框,ArrayList
的大小也是0.你能告诉我为什么会这样吗?
感谢。
P.S。 这是我得到的错误:
[Field error in object 'student' on field 'courseList': rejected value
[mad.nurseryapplication.model.Course@1c92233b,mad.nurseryapplication.model.Course@3e470524];
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'
这是学生班级的一部分。它正确地拥有所有的吸气剂和设定者。
@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",joinColumns=@JoinColumn(name="student_id"),inverseJoinColumns=@JoinColumn(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;
}
答案 0 :(得分:1)
您是否尝试过使用 <form:checkboxes>
代码?
<form:checkboxes items="${avacourses}" path="courseList" />
击> <击> 撞击>
另外,请查看:Spring MVC usage of form:checkbox to bind data和Spring Binding List<Object> to Form:checkboxes。
编辑:刚刚意识到它会打破你的标签 - 由forEach循环填充。