0

サブジェクト クラスと 1 対多の関連付けを持つ学生クラスがあります。

public class Student implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -779930820340758193L;
    private int studentId;
    private String studentName;
    private List<Subject> subjectList;


    public Student(int studentId, String studentName, List<Subject> subjectList) {
        super();
        this.studentId = studentId;
        this.studentName = studentName;
        this.subjectList = subjectList;
    }
    public Student() {
    }
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public List<Subject> getSubjectList() {
        return subjectList;
    }
    public void setSubjectList(List<Subject> subjectList) {
        this.subjectList = subjectList;
    }
    @Override
    public String toString() {
        return "Student [studentId=" + studentId + ", studentName="
                + studentName + ", subjectList=" + subjectList + "]";
    }


}

主題:

public class Subject implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 7135393884889475409L;
    private int subjectId;
    private String subjectName;

    public Subject(int subjectId, String subjectName) {
        super();
        this.subjectId = subjectId;
        this.subjectName = subjectName;
    }
    public int getSubjectId() {
        return subjectId;
    }
    public void setSubjectId(int subjectId) {
        this.subjectId = subjectId;
    }
    public String getSubjectName() {
        return subjectName;
    }
    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + subjectId;
        result = prime * result
                + ((subjectName == null) ? 0 : subjectName.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Subject other = (Subject) obj;
        if (subjectId != other.subjectId)
            return false;
        if (subjectName == null) {
            if (other.subjectName != null)
                return false;
        } else if (!subjectName.equals(other.subjectName))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Subject [subjectId=" + subjectId + ", subjectName="
                + subjectName + "]";
    }

}

私のコントローラーは、ビューでデフォルトでチェックされるように、すべての科目と学生が登録した科目のリストを返します。

コントローラ:

public class HelloController{

   @RequestMapping(value = "", method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      List<Subject> allList = new ArrayList<Subject>();
      allList.add(new Subject(1,"English"));
      allList.add(new Subject(2,"Maths"));
      allList.add(new Subject(3,"Science"));
      allList.add(new Subject(4,"Social"));
      allList.add(new Subject(5,"Tamil")); 

      List<Subject> enrolledList = new ArrayList<Subject>();
      enrolledList.add(new Subject(1,"English")); 
      enrolledList.add(new Subject(3,"Science"));

      Student student = new Student(1,"SRI",enrolledList);
      model.addAttribute("student", student);
      model.addAttribute("availableList", allList);
      return "home";
   }

   @RequestMapping(value = "save" , method = RequestMethod.POST)
   public String saveStudent(@ModelAttribute Student student , ModelMap model){
       System.out.println(">>>>>>>>>>>>"+student);
       model.addAttribute("finalStudent", student); 
       return "success";
   }

home.jsp

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
    <h2>${student.studentName}</h2>
    <form:form method="POST" action="save" modelAttribute="student">
        <form:checkboxes title="Subjects taken" path="subjectList"
            items="${availableList}" itemLabel="subjectName" element="div" />
        <input type = "submit" value = "Save"/>
    </form:form>
</body>
</html>

しかし、このフォームの送信時に、次のエラーが表示されます。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'student' on field 'subjectList': rejected value [Subject [subjectId=1, subjectName=English],Subject [subjectId=3, subjectName=Science]]; codes [typeMismatch.student.subjectList,typeMismatch.subjectList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.subjectList,subjectList]; arguments []; default message [subjectList]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'subjectList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.sri.test.Subject] for property 'subjectList[0]': no matching editors or conversion strategy found]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

誰かが私を助けることができますか?? 前もって感謝します...

4

2 に答える 2