HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<tr th:each="b:${books}" th:if="${b.price ge 3000}">
<!-- <tr th:unless="${#strings.isEmpty(b.title)}"> -->
<!-- <tr th:if="${not #strings.isEmpty(b.title)}"> -->
<td>[[${b.isbn}]]</td>
<td>[[${b.title}]]</td>
<td>[[${b.price}]]</td>
</tr>
</body>
</html>
Book类
public class Book {
private int id;
private String isbn;
private String title;
private int price;
private String attach;
public Book(int id, String isbn, String title, int price, String attach) {
this. id = id;
this. isbn = isbn;
this. title = title;
this. price = price;
this. attach = attach;
}
}
Controller
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
@Controller
public class ViewController {
ArrayList<Book> bookList = new ArrayList<Book>();
@GetMapping("/list")
public String test01(Model model){
int id = 0;
String isbn = "ISN00";
String title = "Sample0";
int price = 2500;
String attach = "cd";
for(int i = 0 ; i < 10 ; i++) {
id = id + 1;
isbn = isbn + 1;
title = title +1;
price = price +100;
if(attach=="cd") {
title = null;
attach = "dvd";
}else {
attach = "dl";
title = "dl";
}
Book book= new Book(id, isbn, title, price, attach);
bookList.add(book);
model.addAttribute("books",bookList);
}
return "list";
}
}
页面上时报500的错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jan 12 10:21:48 CST 2022
There was an unexpected error (type=Internal Server Error, status=500).
HTML上显示Book类的price变量找不到
以为时Maven依赖的问题
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
尝试过以后都没用。
这是日志
2022-01-12 10:48:58.663 ERROR 5652 --- [nio-8080-exec-4] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-4] Exception processing template "list": An error happened during template parsing (template: "class path resource [templates/list.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/list.html]")
2022-01-12 10:48:58.665 ERROR 5652 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/list.html]")] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'price' cannot be found