将 JPA 实体 table 数据显示到 thymeleaf 视图
Displaying JPA Entity table data to thymeleaf view
我有一个带有 id 和 name 列的 JPA 实体 table。我想将名称值显示到 thymeleaf 视图。现在我可以在视图中看到实体对象,但看不到名称的列值。下面是我如何创建 table 并定义 getter 和 setter:
@Entity
@Table(name = "Book")
public class Book extends AbstractPersistable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
public Long getID() {
return this.id;
}
public String getContent() {
return this.name;
}
public void setID (Long id) {
this.id = id;
}
public void setContent (String name) {
this.name = name;
}
}
这就是我在控制器中尝试设置内容的方式:
@Controller
@RequestMapping("/")
public class BookController {
@Autowired
private BookRepository bookRepository;
@RequestMapping("/addBook")
@ResponseBody
public String addBook(@RequestParam("name") String name) {
Book book = new Book();
book.setContent(name);
bookRepository.save(book);
return name;
}
@RequestMapping(value = "book", method = RequestMethod.GET)
public String books(Model model) {
model.addAttribute("books", bookRepository.findAll());
return "book";
}
}
thymeleaf 视图模板:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head lang="en">
<meta charset="UTF-8" />
<title>Books</title>
</head>
<body>
<h1>Books Database</h1>
<ul th:each="book : ${books}">
<li th:text="${book.name}"></li>
</ul>
</body>
</html>
当我从 addBook
页面添加新书并访问该书页以查看书名时,出现此错误:
Exception evaluating SpringEL expression: "book.name". In the IDE
console the error found as:
org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'name' cannot be found on object of type
'sec.book.BookNames.
有什么问题吗?
在视图中,如果我使用 th:text="${book}
,那么 table 中的每一行都会显示一个实体对象列表,如下所示。这是显示的内容:
Entity of type sec.helloworld.Book with id: null.
我只想显示书名而不是对象。我该怎么做?
Yoor getter for name 必须命名为 getName() 才能被 ExpressionLanguage(SpEL = Spring Expression Language) 找到。
setter 也应该是 setName。
我有一个带有 id 和 name 列的 JPA 实体 table。我想将名称值显示到 thymeleaf 视图。现在我可以在视图中看到实体对象,但看不到名称的列值。下面是我如何创建 table 并定义 getter 和 setter:
@Entity
@Table(name = "Book")
public class Book extends AbstractPersistable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
public Long getID() {
return this.id;
}
public String getContent() {
return this.name;
}
public void setID (Long id) {
this.id = id;
}
public void setContent (String name) {
this.name = name;
}
}
这就是我在控制器中尝试设置内容的方式:
@Controller
@RequestMapping("/")
public class BookController {
@Autowired
private BookRepository bookRepository;
@RequestMapping("/addBook")
@ResponseBody
public String addBook(@RequestParam("name") String name) {
Book book = new Book();
book.setContent(name);
bookRepository.save(book);
return name;
}
@RequestMapping(value = "book", method = RequestMethod.GET)
public String books(Model model) {
model.addAttribute("books", bookRepository.findAll());
return "book";
}
}
thymeleaf 视图模板:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head lang="en">
<meta charset="UTF-8" />
<title>Books</title>
</head>
<body>
<h1>Books Database</h1>
<ul th:each="book : ${books}">
<li th:text="${book.name}"></li>
</ul>
</body>
</html>
当我从 addBook
页面添加新书并访问该书页以查看书名时,出现此错误:
Exception evaluating SpringEL expression: "book.name". In the IDE console the error found as: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'name' cannot be found on object of type 'sec.book.BookNames.
有什么问题吗?
在视图中,如果我使用 th:text="${book}
,那么 table 中的每一行都会显示一个实体对象列表,如下所示。这是显示的内容:
Entity of type sec.helloworld.Book with id: null.
我只想显示书名而不是对象。我该怎么做?
Yoor getter for name 必须命名为 getName() 才能被 ExpressionLanguage(SpEL = Spring Expression Language) 找到。 setter 也应该是 setName。