欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

4) Spring Boot 整合 Thymeleaf 模板引擎 之 表达式语法

程序员文章站 2022-05-01 23:14:44
...

注:这里只列举常用的表达式语法,更多其他详细参详thymeleaf官方文档https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

1.使用文本

声明th:*属性的命名空间

<html xmlns:th="http://www.thymeleaf.org">

创建一个属性文件内容如下:

home.welcome=Welcome to our <b>fantastic</b> grocery store!

th:text 设置文本内容

<h1 th:text="#{home.welcome}">标题</h1>

..
<!--得到结果   <b>标签已被转义,因此它将显示在浏览器中-->
<h1>Welcome to our &lt;b&gt;fantastic&lt;/b&gt; grocery store!</h1>

th:utext 设置未转义文本内容

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

...
<!--得到的结果-->
<p>Welcome to our <b>fantastic</b> grocery store!</p>


th:href 用于定义超链接

<a href="details.html" th:href="@{/order/{orderId}/detail(orderId='555',name=${title})}">测一下</a>
<link rel="stylesheet" type="text/css" href="" th:href="@{/css/hover-min.css}"/>

th:src 用于外部资源的引入,例如图片,js文件

<img alt="555" src="thymeleaf.png" th:src="@{https://www.thymeleaf.org/doc/images/thymeleaf.png}"/>
<script type="text/javascript" src="" th:src="@{/js/jquery.min.js}"></script>

 th:attr 用于设置任意属性

<input type="text" name="username" value="占位符" th:attr="value=${title},type='button'"/>

th:id 类似html标签中的id属性

<div class="student" th:id="${user.id}"></div>

 th:value 用于标签赋值,类似<option>标签的value属性。

<input name="username" type="text" value="666" th:value="${username}"/>

 th:action 定义后台控制器路径,类似<form>标签的action属性。

<form id="login-form" action="login.html" th:action="@{/login}">
    <input type="text" name="username"/>
    <input type="submit" value="提交"/>
</form>

th:object 用于数据对象绑定,与后台controller中参数保持一致,和选择(星号)表达式

    .... 
    @RequestMapping("login")
    public String login(User user) {
        System.out.println(user.getName()+","+user.getPassword());
        return "login";
    }
<form id="login-form" action="login.html" th:action="@{/login}" th:object="${user}">
	<input type="text" name="name" th:value="*{name}"/><br/>
	<input type="password" name="password" th:value="*{password}"/><br/>
	<input type="submit" value="提交"/>
</form>

 th:field 常用于表单字段绑定。通常与th:object一起使用。 属性绑定、集合绑定

<form id="login-form" action="login.html" th:action="@{/login}" th:object="${user}">
    <input type="text" th:field="*{name}"/><br/>
    <input type="submit" value="提交"/>
</form>

 thymeleaf里的th:field等同于th:nameth:value,还会生成对应的id

th:if 条件判断(成立执行)

<div th:if="${testList.size()>0}" th:text="${testList.get(0)}"></div>

th:unless 条件判断(不成立执行)

<div th:unless="${testList.size()<0}" th:text="${testList.get(0)}"></div>

 标签只有在th:if中条件成立时才显示,th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

th:switch th:case 用于多个同等级相同目的判断,多选一时使用

<div th:switch="${testList.size()}">
    <p th:case="1" th:text="${testList.get(0)}"></p>
    <p th:case="2" th:text="${testList.get(1)}"></p>
    <p th:case="3" th:text="${testList.get(2)}"></p>
</div>

 th:each 用于遍历集合中的对象,相当于jstl中的<c:forEach>标签

<div th:each="m,i:${testList}">
    <p>
    <span th:text="${m}">临时变量</span>
    <span th:text="${i.count}">迭代对象计数</span>
    <span th:text="${i.index}">迭代对象索引</span>
    <span th:text="${i.even}">当前循环是否偶数</span>
    <span th:text="${i.odd}">当前循环是否奇数</span>
    </p>
</div>

2.标准表达式语法

#{...}:Message 表达式

<p th:text="#{global.title}">外部文本</p>

#{...}:变量表达式

<p th:text="${title}">普通文本</p>

*{...}:选择表达式

	<div th:object="${session.user}">
		<p>name:<span th:text="*{name}"></span></p>
		<p>age:<span th:text="*{age}"></span></p>
	</div>
	<div th:object="${session.user}">
		<p>name:<span th:text="${session.user.name}"></span></p>
		<p>age:<span th:text="*{age}"></span></p>
	</div>
	<div th:object="${session.user}">
		<p>name:<span th:text="${#object.name}"></span></p>
		<p>age:<span th:text="*{age}"></span></p>
	</div>
	<div>
		<p>name:<span th:text="${session.user.name}"></span></p>
		<p>age:<span th:text="${session.user.age}"></span></p>
	</div>
	<div>
		<p>name:<span th:text="*{session.user.name}"></span></p>
		<p>age:<span th:text="*{session.user.age}"></span></p>
	</div>

@{...}:链接表达式

<a href="details.html" th:href="@{/order/{orderId}/detail(orderId='555',name=${title})}">测一下</a>
<img alt="555" src="thymeleaf.png" th:src="@{https://www.thymeleaf.org/doc/images/thymeleaf.png}"/>

~{...}:片段表达式

支持两种语法结构

推荐:~{templatename::fragmentname}

支持:~{templatename::#id}

templatename:模版名,Thymeleaf会根据模版名解析完整路径:/resources/templates/templatename.html,要注意文件的路径。

fragmentname:片段名,Thymeleaf通过th:fragment声明定义代码块,即:th:fragment="fragmentname"

id:HTML的id选择器,使用时要在前面加上#号,不支持class选择器。

 

在我们的模板中,我们经常需要包含其他模板中的部分,页脚,标题,菜单等部分......

为了做到这一点,Thymeleaf需要我们定义这些部分,“片段”,以便包含,这可以使用th:fragment属性来完成

假设要创建一个页脚,因此我们创建一个 resources/templates/footer.html 包含以下代码

同一模板片段引入

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <div th:fragment="copy">
        <h1>测试布局</h1>
        &copy; 2011 The Good Thymes Virtual Grocery
    </div>

    <!--在同一模板片段引入-->
    <div th:insert="~{::copy}"></div>
    <div th:insert="~{footer::copy}"></div>
    <div th:insert="~{this::copy}"></div>
    <div th:insert="::copy"></div>
</body>
</html>

不同模板片段引入

<div th:insert="~{footer::copy}"></div>

没有引用片段 th:fragment

<!-- 由于标记选择器的强大功能,我们可以包含不使用任何th:fragment属性的片段  -->
...
<div id="copy-section">
    &copy; 2011 The Good Thymes Virtual Grocery
</div>
...

<!-- 可以使用上面的片段简单地通过其id属性引用它,类似于CSS选择器  -->
...	
<div th:insert="~{footer::#copy-section}"></div>
...

th:insertth:replace(和th:include)之间的区别

th:insert:将代码块片段整个插入到使用了th:insert的HTML标签中,

th:replace:将代码块片段整个替换使用了th:replace的HTML标签中,

th:include:将代码块片段包含的内容插入到使用了th:include的HTML标签中(thymeleaf3.0不推荐)

采用官方例子说明三者的区别 :

<footer th:fragment="copy">
  &copy; 2011 The Good Thymes Virtual Grocery
</footer>

 三种引入方式:

<body>

  ...

  <div th:insert="footer :: copy"></div>

  <div th:replace="footer :: copy"></div>

  <div th:include="footer :: copy"></div>
  
</body>

显示的结果如果:

<body>

  ...
 <!--th:insert是在div中插入代码块,即多了一层div-->
  <div>
    <footer>
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>
   
<!--th:replace是将代码块代替当前div,其html结构和之前一致-->
  <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>

<!--th:include是将代码块footer的内容插入到div中,即少了一层footer-->
  <div>
    &copy; 2011 The Good Thymes Virtual Grocery
  </div>
  
</body>

3.字符串拼接 

<span th:text="'The name of the user is ' + ${title}"></span>
<span th:text="|Welcome to our application, ${title}!|"></span>

4.算术运算

支持+-*/%

5.逻辑运算

gt>),lt<),ge>=),le<=),not!) ,eq==),neqne!=

 

相关标签: thymeleaf