ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

千峰商城-springboot项目实战07-Thymeleaf基本语法

2022-04-26 18:02:31  阅读:155  来源: 互联网

标签:07 color 标签 Thymeleaf th 内联 图书 springboot


如果要在Thymeleaf模板中获取从控制传递的数据,需使用th标签。

 

1.在Thymeleaf模板页面引入th标签的命名空间。

test.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
test
<hr/>
<label th:text="${book.bookName}">图书名称</label>
</body>
</html>

 

 

2.在Thymeleaf模板页面取值。

th:text

在几乎所有的HTML双标签都可以使用 th:text 属性,将接收到的数据显示在标签的内容中。

text.html:

价格:<label th:text="${price}"></label> <br>
字符串:<div th:text="${str}"></div>
<p th:text="${book.bookName}"></p> <!--${book.bookName}:OGNL 对象图导航语言-->

 

th:inline   内联标签

HTML内联

<p th:inline="text">图书名称:[[${book.bookName}]]</p> 

CSS内联:

<!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>
test
<hr/>

价格:<label th:text="${price}"></label> <br>
字符串:<div th:text="${str}"></div>
<p th:text="${book.bookName}"></p>  <!--${book.bookName}:OGNL 对象图导航语言-->

<p th:inline="text" class="style1">图书名称:[[${book.bookName}]]</p>  <!--${book.bookName}:OGNL 对象图导航语言-->

</body>
</html>

 

 JavaScript内联:

<script type="css/javascript" th:inline="javascript">

</script>

 

th:object 和 *

 

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

 

 

 

3.流程控制

 

th:each  循环

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

 

 th: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>
            <td th:if="${b.bookPrice}>40" style="color: red">太贵!!!</td>
            <td th:if="${b.bookPrice}<=40" style="color: green">推荐购买</td>
        </tr>
    </tbody>
</table>

 

 

 

 th:swich 和 th:case  分支

<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="1">建议购买</label>
                <label th:case="3">价格合理</label>
                <label th:case="*">价格不合理</label>
            </td>
        </tr>
    </tbody>
</table>

 

 

 

 

 

标签:07,color,标签,Thymeleaf,th,内联,图书,springboot
来源: https://www.cnblogs.com/lysboke/p/16195824.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有