springboot中thymeleaf的使用

建立工程前引入thymeleaf依赖

在templates中建html文件

在文件头加<html lang="en" xmlns:th="http://www.thymeleaf.org">

简单三步就可以使用thymeleaf了

1、展示基本元素

        对象属性:<label th:text="${book.bookName}">图书名称(取不到值就会显示我)</label><br>
        数字:<span th:text="${price}">价格</span><br>
        字符串:<span th:text="${str}">很久以前</span><br>
        对象:<span th:text="${book}">书本</span><br>

2、在style中使用

    <style type="text/css" th:inline="css">
        .style1{
            color: [[${color}]];
        }
    </style>

    书本名:<span class="style1" th:text="${book.bookName}">书本名:</span><br>

 3、显示时不替换行内字符

<span th:inline="text">书本名:[[${book.bookName}]]</span>

 4、显示对象

    <div th:object="${book}">
        <p th:text="*{bookId}"/>
        <p th:text="*{bookName}"/>
        <p th:text="*{bookAuthor}"/>
        <p th:text="*{bookPrice}"/>
    </div>

5、用each遍历

<table style="width: 600px;" border="1" cellspacing="0">
        <caption>图书信息列表</caption>
        <thead>
            <tr>
                <th>图书ID</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}"/>
            </tr>
        </tbody>
    </table>

 6、用if判断

<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 th:if="${b.bookPrice>40}" style="color: red;">太贵了!</td>
            <th th:if="${b.bookPrice<=40}" style="color: darkgreen;">便宜!!</th>
        </tr>
        </tbody>
    </table>

7、用switch判断

<table style="width: 600px;" border="1" cellspacing="0">
        <caption>图书信息列表</caption>
        <thead>
        <tr>
            <th>图书ID</th>
            <th>图书名称</th>
            <th>作者</th>
            <th>价格</th>
            <th>购买建议switch</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 th:switch="${b.bookPrice/10}">
                <label th:case="3">价格便宜</label>
                <label th:case="4">价格合理</label>
                <label th:case="5">价格太贵</label>
                <label th:case="6">价格太贵</label>
            </td>
        </tr>
        </tbody>
    </table>

8、碎片使用

 a当主页面,footer和header当碎片

所有页面都要加   <html lang="en" xmlns:th="http://www.thymeleaf.org">

header

<div th:fragment="fragment1" style="width: 100%;height: 80px;background: deepskyblue; color: white; font-size: 25px;">
    #############header##############
</div>

footer

<div th:fragment="fragment2" style="width: 100%;height: 30px;background: lightblue; color: white; font-size: 16px;">
    #############footer###########
</div>

a (使用include,不会引入style)

<div th:include="header::fragment1"></div>
<div style="width: 100%; height: 500px;">
    定义内容
</div>
<div th:include="footer::fragment2"></div>

 a (使用replace,会引入style)

<div th:replace="header::fragment1"></div>
<div style="width: 100%; height: 500px;">
    定义内容
</div>
<div th:replace="footer::fragment2"></div>

 

总结:注意在文件头加xmlns,会用th:text   th:each   th:if     th:switch  th:replace,基本就够了

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot使用Thymeleaf是相对简单的,你只需要在你的项目加入Thymeleaf的相关依赖即可。使用Thymeleaf步骤如下: 1. 在你的项目添加Thymeleaf的依赖。可以通过Maven或者Gradle来添加依赖。例如,在Maven,你可以在`pom.xml`文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在你的Spring Boot应用的配置文件,配置Thymeleaf的一些属性。你可以在`application.properties`文件添加下面的配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false ``` 在这个例子,我们设置了模板文件的前缀为`classpath:/templates/`,后缀为`.html`,并且将缓存关闭。 3. 创建Thymeleaf模板文件。在`src/main/resources/templates/`目录下创建你的HTML模板文件。在这些模板文件,你可以使用Thymeleaf的标记来进行动态数据绑定和渲染。 4. 在你的Spring Boot应用使用Thymeleaf。你可以在Controller的方法返回一个字符串,该字符串指定了你要渲染的Thymeleaf模板文件的名称。Spring Boot会自动根据配置的前缀和后缀来查找并渲染对应的模板文件。 以上就是在Spring Boot使用Thymeleaf的简单步骤。通过这些步骤,你可以在你的Spring Boot应用使用Thymeleaf来进行视图的渲染和展示。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youngcave2

等待第一笔打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值