Thymeleaf怎么调用静态资源的看我之前发过的文章
这个首先在controller创建一个book的类,book的一些属性自己定义记得getsetyi'x
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/query")
public String queryBook(int bookId,Model model){
Book book = new Book(bookId, "语文", "龙哥",1);
List<Book> books = new ArrayList<>();
books.add(new Book(1,"java","龙哥1",1));
books.add(new Book(2,"C++","龙哥2",1));
books.add(new Book(3,"C#","龙哥3",61));
books.add(new Book(4,"Python","龙哥4",31));
books.add(new Book(5,"GO","龙哥5",100));
model.addAttribute("books",books);
return "test";
}
}
下面是test.html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css" th:inline="css">
.style1{
color:[[${color}]]
}
</style>
</head>
<body>
<hr/>
价格:<label th:text="${price}">价格</label><br/>
字符串:<div th:text="${str}"></div>
<p th:inline="text" class="style1">图书名称:[[${book.bookName}]]</p>
<div th:object="${book}">
<p th:text="*{bookId}"></p>
<p th:text="*{bookName}"></p>
<p th:text="*{bookAuthor}"></p>
</div>
<table style="width: 600px" border="1" cellspacing="0">
<caption>图书信息列表</caption>
<thead>
<tr>
<th>图示ID</th>
<th>图示名称</th>
<th>图示作者</th>
<th>图示价格</th>
<th>购买建议</th>
</tr>
</thead>
<tbody>
<tr th:each="b:${books}">
<td th:text="${b.bookId}"></td>
<td th:text="${b.bookName}"></td>
<td th:text="${b.bookAuthor}"></td>
<td th:text="${b.bookPrice}"></td>
<!-- <td th:if="${b.bookPrice}>40" style="color: red">太贵!!!</td>-->
<!-- <td th:if="${b.bookPrice}<=40" style="color: green">推荐购买^_^</td>-->
<td th:switch="${b.bookPrice}/10">
<label th:case="3">价格合理,建议购买</label>
<label th:case="4">价格太贵,不建议购买</label>
<label th:case="*">价格不合理,谨慎选择</label>
</td>
</tr>
</tbody>
</table>
</body>
</html>
下面是运行结果