【发布时间】:2016-07-18 14:57:41
【问题描述】:
我是 thymeleaf & spring boot 的新手。我正在使用 thymeleaf/bootstrap 和 springboot 为学生和学生课程创建一个主详细信息页面。
当我运行页面并单击保存按钮时,它会抛出错误。
错误:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studentCourse' available as request attribute
我确定我没有正确链接 studentCourse,但不知道如何解决这个问题。请问有人可以帮忙吗?
学生.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;
}
学生.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
....
....
【问题讨论】:
标签: twitter-bootstrap spring-boot thymeleaf