2016-07-18 9 views
0

私はthymeleaf &スプリングブートを初めて使用しています。私はthymeleaf/bootstrapとspringbootを使って生徒や学生のコースのマスター詳細ページを作成しています。タイムリーフ&スプリングブートを使用してマスター詳細ページを作成する際にエラーが発生しました

ページを実行して保存ボタンをクリックすると、以下のエラーが表示されます。

エラー:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studentCourse' available as request attribute 

私は正しくなく、この問題を解決する方法がわからstudentCourseを結ぶわけではないと確信しています。誰か助けてもらえますか?

Student.html

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> 
<html xmlns:th="http://www.thymeleaf.org" 
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
    layout:decorator="layout"> 
<head> 
<title>Students</title> 
</head> 
<body> 
    <div layout:fragment="content"> 

     <div class="row"> 
      <div class="col-md-offset-3 col-md-6"> 
       <div class="panel panel-success"> 
        <div class="panel-heading"> 
         <h3 class="panel-title">Add New Student</h3> 
        </div> 
        <div class="panel-body"> 
         <form th:object="${student}" th:action="@{/student/create}" 
          action="#" method="post"> 
          <input type="text" th:field="*{studentId}" class="form-control" placeholder="Student Id" /> 
          <div style="clear: both; display: block; height: 10px;"></div> 
          <input type="text" th:field="*{firstName}" class="form-control" placeholder="First Name" /> 
          <div style="clear: both; display: block; height: 10px;"></div> 
          <input type="text" th:field="*{lastName}" class="form-control" placeholder="Last Name" /> 
          <div style="clear: both; display: block; height: 10px;"></div> 
          <input type="text" th:field="*{city}" class="form-control" placeholder="City" /> 
          <div style="clear: both; display: block; height: 10px;"></div> 
          <input type="submit" class="btn btn-success pull-right" 
           value="Save"> 
         </form> 
        </div> 
       </div> 
      </div> 

     </div> 
     <!-- start of detail section --> 
     <div class="row" style="margin-bottom: 50px;"> 
      <div class="col-md-offset-2 col-md-8"> 
       <div class="panel panel-success"> 
        <div class="panel-heading"> 
         <h3 class="panel-title">Student Course</h3> 
        </div> 
        <div class="panel-body"> 
         <table class="table table-bordered table-striped table-hover"> 
          <thead> 
           <tr class="btn-success"> 
            <th>Course</th> 
            <th>End Date</th> 
           </tr> 
          </thead> 
          <tbody th:object="${studentCourse}"> 
           <tr> 
            <td><input class="form-control" th:field="*{courseId}" type="text" placeholder="Course" /></td> 
            <td><input class="form-control" th:field="*{endDate}" type="text" placeholder="End Date" /></td> 
            <td> 
             <button class="btn btn-danger btn-remove" type="button"> 
              <i class="glyphicon glyphicon-minus gs"></i> 
             </button> 
            </td> 
           </tr> 
          </tbody> 
         </table> 
         <button class="btn btn-success btn-add" type="button"> 
          <i class="glyphicon glyphicon-plus gs"></i> <b> Add</b> 
         </button> 
        </div> 

       </div> 
      </div> 
     </div> 
     <!-- end of detail --> 
    </div> 
</body> 
</html> 

StudentController.java

@RequestMapping(value="/student/create", method = RequestMethod.POST) 
public ModelAndView create(Student student, List<StudentCourse> studentCourse, BindingResult result) { 

    student.setCourseList(studentCourse); 

    studentService.saveStudent(student); 

    ModelAndView mv = new ModelAndView(); 

    mv.setViewName("/Student"); 

    return mv; 
} 

Student.java

@Entity 
@Table(name="student") 
public class Student { 

    @Id 
    @Column(name="student_id") 
    @GeneratedValue(strategy=GenerationType.AUTO, generator="student_seq_gen") 
    @SequenceGenerator(name="student_seq_gen", sequenceName="SEQ_STUDENT") 
    private Long studentId; 

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "student") 
    private List<StudentCourse> studentCourse; 

    @Column(name="first_name") 
    private String firstName; 

    @Column(name="last_name") 
    private String lastName; 

    @Column(name="gender") 
    private char gender; 

    @Column(name="date_of_birth") 
    private Date dateOfBirth; 

    @Column(name="email") 
    private String email; 

    @Column(name="city") 
    private String city;  

    //getter and setters 
    .... 
    .... 

StudentCourse.java

@Entity 
@Table(name="student_course") 
public class StudentCourse { 

    @Id 
    @Column(name="student_course_id") 
    @GeneratedValue(strategy=GenerationType.AUTO, generator="student_course_seq_gen") 
    @SequenceGenerator(name="student_course_seq_gen", sequenceName="SEQ_STUDENT_COURSE") 
    private Long studentCourseId; 

    @JoinColumn(name="student_id") 
    @ManyToOne 
    private Student student; 

    @Column(name="course_id") 
    private Long courseId; 

    @Column(name="end_date") 
    private Date endDate; 

    //getter and setters 
    .... 
    .... 

答えて

0

<form>タグ内にstudentCourseオブジェクトが含まれていないため、エラーが発生します。 th:fieldを使用して、studentCourseオブジェクトを生徒オブジェクトと同じフォーム要素内に囲む必要があります。 studentCoursestudentオブジェクトのフィールドなので、th:object="studentCourse"を宣言しないでください。

あなたのHTMLは次のようになります、

<body> 
<div layout:fragment="content"> 
<form th:object="${student}"> 
    <!-- access all the student object fields here --> 
    <div class="row"> 
     <input type="text" th:field="*{studentId}" class="form-control" placeholder="Student Id" /> 
    </div> 
    <!-- start of detail section --> 
    <div class="row" style="margin-bottom: 50px;"> 
     <!-- some html code block --> 
     <tbody> 
      <tr> 
       <td><input class="form-control" th:field="*{studentCourse.courseId}" type="text" placeholder="Course" /></td> 
       <td><input class="form-control" th:field="*{studentCourse.endDate}" type="text" placeholder="End Date" /></td> 
       <td> 
       <button class="btn btn-danger btn-remove" type="button"> 
        <i class="glyphicon glyphicon-minus gs"></i> 
       </button> 
       </td> 
      </tr> 
     </tbody> 
     <!-- some html code block --> 
    </div> 
</form> 
</div> 
</body> 

読む:

I have problem in binding the list of objects contained inside a object on the form using thymeleaf

関連する問題