使用thymeleaf&创建主详细信息页面时出错春天的靴子

时间:2016-07-18 14:57:41

标签: twitter-bootstrap spring-boot 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
    ....
    ....

1 个答案:

答案 0 :(得分:0)

您收到错误,因为studentCourse对象未包含在<form>标记内。您需要使用studentCourseth:field对象与学生对象放在同一表单元素中。 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