springboot(7)- 整合視圖層(2)-thymeleaf

1 Thymeleaf

新一代模板引擎

1.1 新建工程

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

1.2 實體類

在這裏插入圖片描述

1.3 Controller

@Controller
public class BookController {

    @GetMapping("/getBook")
    public String getBook(Model model){
        List<Book> bookList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Book book = new Book();
            book.setId(i);
            book.setName("哈利波特—"+i);
            book.setAuthor("JK--"+i);
            book.setPrice((i*10.0));
            bookList.add(book);
        }

        model.addAttribute("books", bookList);

        return "book";
    }
}

1.4 頁面

在這裏插入圖片描述

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <tr>
        <td>圖書編號</td>
        <td>圖書名稱</td>
        <td>圖書作者</td>
        <td>圖書價格</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.name}"></td>
        <td th:text="${book.author}"></td>
        <td th:text="${book.price}"></td>
    </tr>

</table>
</body>
</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章