gpt4 book ai didi

java - 评估 SpringEL 表达式的异常,Spring Guru 类(class)

转载 作者:行者123 更新时间:2023-12-02 01:54:55 24 4
gpt4 key购买 nike

Book.java

package pl.spring.guru.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String isbn;
@OneToOne
private Publisher publisher;

@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name =
"book_id"), inverseJoinColumns = @JoinColumn(name = "author_id"))

private Set<Author> authors = new HashSet<>();

public Book() {
}

public Book(String title, String isbn, Publisher publisher){
this.title=title;
this.isbn=isbn;
this.publisher=publisher;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public Publisher getPublisher() {
return publisher;
}

public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}

public Set<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
}

BookController.java

package pl.spring.guru.spring5webapp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pl.spring.guru.spring5webapp.repositories.BookRepository;

@Controller
public class BookController {

private BookRepository bookRepository;

public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@RequestMapping(value = "/books")
public String getBooks(Model model){
model.addAttribute("books",bookRepository.findAll());

return "books";
}
}

books.html - thymeleaf 模板

<!DOCTYPE html>
<html lang="en" xmlns:th=”http://www.thymeleaf.org”>
<head>
<meta charset="UTF-8">
<title>Spring Framework Guru</title>
</head>
<body>
<h1>Books list</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}">123</td>
<td th:text="${book.title}">Spring in Action</td>
<td th:text="${book.publisher.name}">Wrox</td>
</tr>
</table>

</body>
</html>

当我尝试访问 localhost/books 时,我得到一个

Exception evaluating SpringEL expression: "book.id" (template: "books" - line 16, col 13)

我刚刚学习spring,开始使用spring框架大师教程。我也不明白,为什么需要在 book.java 中添加一个空的构造函数。没有它我就会遇到下一个问题:

No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : pl.spring.guru.spring5webapp.model.Book

最佳答案

您缺少 id 的 getter。如果没有它,模板引擎就无法访问它。

至于默认构造函数 - Spring 需要它来创建实体的实例。如果没有可用的默认构造函数,Spring 如何知道要使用哪些参数。

除了本教程之外,您还应该查看 Spring 框架引用文档。需要阅读的内容有很多,但尤其是 DI 基础知识,解释得非常好。

关于java - 评估 SpringEL 表达式的异常,Spring Guru 类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52455245/

24 4 0