2

我试图通过与Spring 中courseList的对象绑定来从 JSP 页面获取。student下面是我的Student课。但是,即使我选中复选框,大小ArrayList也是 0。你能告诉我为什么会这样吗?

谢谢。

PS这是我得到的错误:

[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'

这是学生课的一部分。它具有正确的所有 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",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;
    }
4

1 回答 1

1

您是否尝试过使用<form:checkboxes>标签?

<form:checkboxes items="${avacourses}" path="courseList" />

另外,看看:Spring MVC 使用 form:checkbox 绑定数据Spring Binding List<Object> to Form:checkboxes

编辑:刚刚意识到它会破坏你的标签 - 由 forEach 循环填充。

于 2012-09-18T11:12:21.940 回答