I am getting the error:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'description' available as request attribute.
Could you help me correct this error?
This is the code where I tried to add fields from my Review model to the Thymeleaf reviewBook.html form. I'm unsure how to add the description and review fields from my Reviews model to the Thymeleaf form. I was trying to create a form for a user to write a review, but one book can have many reviews, so I thought I needed to use the Book entity as the basis for the form. Is this correct?
BookController.java
@Controller
public class BookController {
public BookController() {
}
@Autowired
private AppUserRepository appUserRepository;
@Autowired
private BookRepository bookRepository;
@Autowired
private ReviewRepository reviewRepository;
@Autowired
private RatingRepository ratingRepository;
@GetMapping("/reviewBook")
public String showReviewForm(Model model) {
Book book = new Book();
Review review = new Review();
List<Rating> ratings = ratingRepository.findAll();
model.addAttribute("book", new Book());
model.addAttribute("review", new Review());
model.addAttribute("allRatings", ratings);
return "reviewBook";
}
reviewBook.html
<div class=" container py-5" >
<div class="row">
<div class="col-lg-6 rounded border p-3" >
<h2 class="text-center mb-3">Create Review</h2>
<hr />
<div th:if="${success}" class="alert alert-success alert-
dismissable fade show" role="alert">
<strong>Review Created Successfully!</strong>
</div>
<form method="post" th:object="${book}">
<input type="hidden" th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" />
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="title">Title*</label>
<div class="col-sm-8 ">
<input class="form-control" type="text"
id="title" name="title" th:field="${book.title}">
<p th:if="${#fields.hasErrors('title')}"
th:errorclass="text-danger"
th:errors="${book.title}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="aFirstName">Author's First Name*</label>
<div class="col-sm-8 ">
<input class="form-control" type="text"
id="aFirstName" name="aFirstName" th:field="${book.aFirstName}" >
<p th:if="${#fields.hasErrors('aFirstName')}"
th:errorclass="text-danger"
th:errors="${book.aFirstName}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="aLastName">Author's Last Name*</label>
<div class="col-sm-8 ">
<input class="form-control" type="text"
id="aLastName" name="aLastName" th:field="${book.aLastName}" >
<p th:if="${#fields.hasErrors('aLastName')}"
th:errorclass="text-danger"
th:errors="${book.aLastName}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="description">Brief Description*</label>
<div class="col-sm-8 ">
<textarea class="form-control" type="text"
placeholder="Enter review..," id="description"
name="description" th:field="*{description}"></textarea>
<p th:if="${#fields.hasErrors('description')}"
th:errorclass="text-danger"
th:errors="*{description}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="review">Review*</label>
<div class="col-sm-8 ">
<input class="form-control" type="text"
id="review" name="review" th:field="*{review}" >
<p th:if="${#fields.hasErrors('review')}"
th:errorclass="text-danger"
th:errors="*{review}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label"
th:for="rating">Rating*</label>
<div class="col-sm-8 ">
<select th:field="*{ratings}" multiple="multiple">
<option th:each="rating: ${allRatings}"
th:value=${rating.ratingId}
th:text=${rating.rating}>
</option>
</select>
</div>
</div>
<div class="row mb-3">
<div class="offset-sm-4 col-sm-4 d-grid">
<button type="submit" class="btn btn-
primary">Submit</button>
</div>
<div class="col-sm-4 d-grid">
<a href="/" class="btn btn-outline-
primary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
Book.java
@Entity
@Table(name="books")
public class Book {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@NotEmpty
private String title;
private String aFirstName;
private String aLastName;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
private List<Review> review;
Review.html
@Entity
@Table(name="reviews")
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotEmpty
private String description;
@NotEmpty
private String review;
private Date revDate;
@ManyToOne
@JoinColumn(name= "appUser_id")
private AppUser appUser;
@ManyToOne
@JoinColumn(name="book_id")
private Book book;
@ManyToOne
@JoinColumn(name="rating_id")
private Rating rating;
Do you have any suggestions on how I can correct this error? Is my error with the controller, the form, or the model relationships?