Spring Boot 2.0 从入门到精通-thymeleaf集成

时间:2022-10-27 00:38:42

看本文之前,最好先阅读下此文 Spring Boot 2.0 从入门到精通-QuickStart-1

本文主要目标是在Spring Boot 项目中如何引入thymeleaf 以及bootstrap 来渲染出一个用户列表,文章中不涉及 thymeleaf 和 bootstrap 的详细细节,如感兴趣请见官网:http://www.thymeleaf.org  ,https://getbootstrap.com

在pom.xml 添加thymeleaf 的starter 依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在resources/templates 目录下创建 users.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>用户列表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
</head>

<body>
<table class="table">
    <thead>
    <tr>
        <th scope="col">#</th>
        <th scope="col">First</th>
        <th scope="col">Last</th>
        <th scope="col">Handle</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <th scope="row">1</th>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
    </tr>
    <tr>
        <th scope="row">2</th>
        <td>Jacob</td>
        <td>Thornton</td>
        <td>@fat</td>
    </tr>
    <tr>
        <th scope="row">3</th>
        <td>Larry</td>
        <td>the Bird</td>
        <td>@twitter</td>
    </tr>
    </tbody>
</table>
</body>

</html>

在 SampleController 添加request 映射

    @RequestMapping("/users")
    public String users(){
        return "users";
    }

启动应用程序,在浏览器访问效果如下:

Spring Boot 2.0 从入门到精通-thymeleaf集成

源码下载

=====================================================

单页表单,简单易用 https://www.dan-ye.com,帮您在线收集各类数据