Spring Boot中的Thymeleaf模板引擎介绍

一、Thymeleaf模板引擎介绍
【1】Thymeleaf 是 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理HTML,XML,JavaScript,CSS 甚至纯文本。
【2】Thymeleaf 的主要目标是提供一种优雅和高度可维护的创建模板的方式。为了实现这一点,它建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板被用作设计原型。这改善了设计的沟通,弥补了设计和开发团队之间的差距。
【3】Thymeleaf 也从一开始就设计了Web标准 - 特别是 HTML5 - 允许您创建完全验证的模板,Spring Boot 官方推荐使用 thymeleaf 而不是 JSP。

二、Spring Boot中的Thymeleaf模板引擎
【1】引入Thymeleaf,在pom.xml文件中导入以下依赖

<!-- 引入thymeleaf模板引擎-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

注意:不需要在依赖中写入版本号,Spring Boot已经帮我们统一号版本。

【2】Thymeleaf的使用和语法

1、把HTML页面放在classpath:/templates路径下,thymeleaf就能帮我们自动渲染。
2、thymeleaf会帮我们自动在后缀名字加上.html,并且在路径classpath:/templates下找到success.html页面,和Spring MVC中的视图解析器类似。

举一个例子:
①、在controller包下创建HelloWorldController类,如下所示:

package com.czd.springbootwebdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author czd
 */
@Controller
public class HelloWorldController {

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

②、在 resources/templates 路径下创建 success.html,页面代码如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Success</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

③、运行Spring Boot,也就是main方法有SpringApplication.run(SpringbootwebdemoApplication.class, args)的程序驱动类,如下所示:

package com.czd.springbootwebdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootwebdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebdemoApplication.class, args);
    }

}

④、输出结果如下图:
在这里插入图片描述
三、thymeleaf的语法

①、导入thymeleaf的名称空间,如下所示:

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

②、基本语法

  • th:text:改变当前元素里面的文本内容,例如< div th:text="${hello}" >,作用是将div里面的值设置为hello中的值。
  • th:insert / th:replace:片段包含,类似于JSP中的 jsp:include。
  • th:each:遍历,类似于JSP中的c:forEach。
  • th:if / th:unless / th:switch / th:case:条件判断。
  • th:object / th:with:声明变量。
  • th:attr / th:attrprepend / th:attrappend:任意属性修改,支持prepend,append。
  • th:value / th:href / th:ssrc:修改指定属性。
  • th:text / th:utext:修改表情内容。
  • th:fragment:声明片段。

③、表达式

  • 简单表达式:
  1. ${…}:获取变量值。
  2. *{…}:选择表达式。
  3. #{…}:获取国际化内容。
  4. @{…}:定义URL。
  5. ~{…}:片段引用表达式。

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

  • 字面量:
  1. 字符串: ‘one text’ , ‘Another one!’。
  2. 数字: 0 , 34 , 3.0 , 12.3 。
  3. .布尔值: true , false。
  4. .Null 值: null。
  5. 多个数据用逗号隔开: one , sometext , main。

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

  • 文本操作:
  1. 加号: +。
  2. 替换: |The name is ${name}|。

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

  • 数学运算:
  1. 二元操作符: + , - , * , / , %
  2. 负号: -
  3. .布尔值: true , false。
  4. .二元操作符: and , or
  5. 布尔否定: ! , not

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

  • 比较与平等运算:
    1.比较符号: > , < , >= , <= ( gt , lt , ge , le )。
  1. 平等符号: == , != ( eq , ne )

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

  • 条件运算:
  1. If-then: (if) ? (then)
  2. If-then-else: (if) ? (then) : (else)
  3. .Default: (value) ?: (defaultvalue)

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

四、代码示例
【1】在 classpath/templates 路径下创建 success.html页面,代码如下所示:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Success</title>
</head>
<body>
<h1>success</h1>
<!-- th:text 将div里面的值设置为hello中的值 -->
<div th:text="${hello}">不对特殊字符进行转义!</div>
<div th:utext="${hello}">对特殊字符进行转义!</div>
<!-- th:each 每次遍历都会生成当前标签-->
<h4 th:text="${user}" th:each="user:${users}">这里是users集合的内容</h4>
</body>
</html>

【2】创建一个controller包,在此包下创建HelloWorldController类,如下所示:

package com.czd.springbootwebdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.rmi.transport.ObjectTable;

import java.util.Arrays;
import java.util.Map;

/**
 * @author czd
 */
@Controller
public class HelloWorldController {

    @RequestMapping("/success")
    public String success(Map<String, Object> map){
        map.put("hello","<h1>HelloWorld</h1>");
        map.put("users", Arrays.asList("张三","李四","王五"));
        return "success";
    }
}

【3】运行Spring Boot程序驱动类,输出结果如下图所示:
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值