gpt4 book ai didi

java - 在 Spring 中实现 thymeleaf 时无法访问对象的属性

转载 作者:行者123 更新时间:2023-12-02 11:03:53 25 4
gpt4 key购买 nike

我似乎无法访问传递到 thymeleaf 中 html 文件的对象的属性。我正在构建一个 Spring REST Web 应用程序。

Controller 中的代码

@RequestMapping(value = {"/home/books"}, method = RequestMethod.GET)
public ModelAndView books()
{
ModelAndView modelAndView=new ModelAndView();
Book book =new Book("name","author","id","54");
//book.addBook(book("name","author","id","54"));
List<Book> AllBook =bookService.getAllBooks();
AllBook.add(book);
modelAndView.addObject("books",AllBook);
modelAndView.setViewName("books");
return modelAndView;
}

我的模板中的 books.html 文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Books</title>
</head>
<body>

<tr th:each="book: ${books}">
<h1> <td th:text="${book.author}" /></h1>
<td th:text="${book.name}" />
</tr>
</body>
</html>

POJO类

Public class Book implements Serializable{
@Id
private String id;
private String name;
private String author;
private String price;
private int quantity;

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public Book(String name, String author, String id, String price) {
super();
this.name = name;
this.author = author;
this.id = id;
this.price = price;
}
public Book()
{

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}
}

该网页不显示 book.author 或 book.name 的值。 IT 应该显示name , 作者。页面本身正在响应,但未显示任何值。显示此错误出现意外错误(类型=内部服务器错误,状态=500)。计算 SpringEL 表达式时出现异常:“book.name”(模板:“books” - 第 11 行,第 9 栏)

我在使用 thymeleaf 时犯了什么错误吗?请帮忙

最佳答案

POJO 中定义公共(public) settergetters

@Entity
public class Book{
@Id
private String id;
private String name;
private String author;



public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

private String price;
private int quantity;

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public Book() {

}
public Book(String id, String name, String author, String price, int quantity) {
super();
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.quantity = quantity;
}



}

books.html 文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Books</title>
</head>
<body>
<table>
<tr th:each="book: ${books}">
<h1>
<td th:text="${book.author}"></td>
</h1>
<td th:text="${book.name}"></td>
</tr>
</table>
</body>
</html>

图书库

public interface Bookrepository extends JpaRepository<Book, String>{    

}

图书服务

public interface BookService {

public List<Book> getALlBooks();

public void addBook(Book book);
}

BookService 实现

@Service
public class BookServiceImpl implements BookService {

@Autowired
Bookrepository bookrepository;

@Override
public List<Book> getALlBooks() {

return bookrepository.findAll();
}

@Override
public void addBook(Book book) {

bookrepository.save(book);
}

}

你的 Controller

@Controller("/")
public class BooksController{

@Autowired
BookService bookService;

@RequestMapping(value = "home/books", method = RequestMethod.GET)
public ModelAndView books() {
ModelAndView modelAndView = new ModelAndView();
Book book = new Book("id", "name1", "author1", "54", 5);
Book book2 = new Book("id 2", "name2", "author2", "54", 4);

bookService.addBook(book);
bookService.addBook(book2);

List<Book> AllBook = bookService.getALlBooks();


// Check here if list is empty and throw an exception

modelAndView.addObject("books", AllBook);
modelAndView.setViewName("books");
return modelAndView;
}
}

说明

你的第一个问题是因为你没有定义settersgetter而引起的。您有私有(private)字段,无法在类(class)之外访问它们。

你的第二个问题是因为你传递了空列表而引起的。确保在传递变量之前检查它们。一个简单的记录器不会给你带来任何坏处。大多数时候,它非常方便。

关于java - 在 Spring 中实现 thymeleaf 时无法访问对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51132349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com