我似乎无法访问百里香传递给html文件的对象的属性。我正在构建一个Spring REST Web应用程序。
在控制器中编码
@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,author。页面本身正在响应,但未显示任何值。 显示此错误 发生意外错误(类型=内部服务器错误,状态= 500)。 评估SpringEL表达式的异常:“ book.name”(模板:“ books”-第11行,第9行)
我用百里香做过任何错误吗? 请帮助
答案 0 :(得分:1)
setters
中定义公共getters
和POJO
@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;
}
}
<!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);
}
@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("/")
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;
}
}
您的第一个问题是由于未定义setters
和getter
而引起的。您有私有字段,无法在课堂外访问它们。
第二个问题是由于您传递了空列表而引起的。确保在传递变量之前检查变量。简单的logger
不会对您有任何不利影响。在大多数情况下,它非常方便。
答案 1 :(得分:0)
确定您的html文件名是'book.html'而不是'books.html'?
如果是book.html,则应设置视图名称modelAndView.setViewName("book");
否则将文件名更改为books.html并将视图设置为modelAndView.setViewName("books");