2

I am trying to get courseList from a JSP page by binding with the student object in Spring. Below is my Student class. However even if I check the checkboxes, the size of the ArrayList is 0. Can you please tell me why this is happening?

Thanks.

P.S. This is the error I am getting:

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

This is a part of student class. it has all the getters and setters correctly.

@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>();

This is my jsp page code.

<%@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>

Below is my controller code that handle it.

@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;
    }
1
  • You should check the binding errors with if (result.hasErrors()) { //log it } Commented Sep 18, 2012 at 11:09

1 Answer 1

1

Have you tried using the <form:checkboxes> tag instead?

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

Also, take a look at: Spring MVC usage of form:checkbox to bind data and Spring Binding List<Object> to Form:checkboxes.

EDIT: just realised it will break your labels - being populated by the forEach loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.