SpringBoot集成Thymeleaf模板引擎

一、Thymeleaf简介

Thymeleaf是一个Java XML / XHTML / HTML5 模板引擎 ,可以在Web(基于servlet )和非Web环境中工作。它更适合在基于MVC的Web应用程序的视图层提供XHTML / HTML5,但它甚至可以在脱机环境中处理任何XML文件。它提供完整的Spring Framework。

在Web应用程序中,Thymeleaf旨在成为JavaServer Pages(JSP)的完全替代品,并实现自然模板的概念:模板文件可以直接在浏览器中打开,并且仍然可以正确显示为网页。

 

二、集成Thymeleaf

1. 添加Thymeleaf依赖

首先在项目中增添thymeleaf依赖spring-boot-starter-thymeleaf
同时为了解决html严格校验报错的问题,增添依赖nekohtml

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

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.15</version>
</dependency>

 

2. 添加Thymeleaf配置

application.properties如下:

##端口号
server.port=8765


##去除thymeleaf的html严格校验
spring.thymeleaf.mode=LEGACYHTML5

#设定thymeleaf文件路径 默认为src/main/resources/templates
spring.thymeleaf.prefix=classpath:/templates/ 
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
# 是否开启模板缓存,默认true
# 建议在开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
# 模板编码
spring.thymeleaf.encoding=UTF-8

 

3. 测试

添加index.html页面

<!DOCTYPE html>
<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>首页</title>
</head>
<body>
    <p th:text="'hello SpringBoot'">hello thymeleaf</p>
</body>
</html>

 

在入口类所在目录建立controller包,新建控制器,写法与SpringMVC一致:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }
}

 

完整项目结构

 

访问:http://127.0.0.1:8765/index

 

三、Thymeleaf 常用语法

官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

 

1. 语法规则

 th:text;改变当前元素里面的文本内容

 th:任意html属性;来替换原生属性的值

th:include:加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容

th:replace:替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div

th:attr 来设置任意属性

th:attrprepend 来追加(不是替换)属性值

th:classappend

th:each每次遍历都会生成当前这个标签

 th:href="@{...}" 替换url

 th:text="${...}"     转译特殊字符,特殊符号原方不动输出

th:utext="${...}    会转译字符,特殊符号被转译后输出结果

 行内写法

[[ ]]等价于th:text      

[( )]等价于th:utext 

 

2.表达式语法

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

消息表达式: #{...}

URL 表达式: @{...}

代码段表达式: ~{...}

 

3. 字面量

文本字面量: 'some text'

 数值字面量: 0, 34, 3.0, 12.3

 布尔值字面量: true, false

 Null 值字面量: null

 Tokens 字面量: one, content, sometext, ...

 

4. 文本操作符

 字符串连接: +

字面量替换: |The name is ${name}|

 

5. 算术操作符

 二元操作符: +, -, *, /, %

 减号(一元操作符): -

 布尔操作符(逻辑操作符)

 二元操作符: and, or

 非(一元操作符): !, not

 

6. 比较操作符

比较: >, <, >=, <= (gt, lt, ge, le)

 相等性: ==, != (eq, ne)

 

7. 条件操作符

if-then: (if) ? (then)

if-then-else: '(if) ? (then) : (else)'

默认: (value) ?: (defaultvalue)

 

8. 特殊符号

 忽略 Thymeleaf 操作: _

 

9. 内置基本对象

 ctx:

 vars

 locale

 request

 response

 session

 servletContext

 

10.内置工具对象

execInfo

messages

uris

conversions

dates

calendars

numbers

strings

objects

bools

arrays

lists

sets

maps

aggregates

ids

 

11. Link url

 th:href="@{/}"返回首页

th:href="@{/thymeleaf/demo1}"跳转demo1 页面

th:href="@{/thymeleaf/demo1(username=${employees[0].name})}">去 demo1 页面, 带参数

th:href="@{/thymeleaf/demo1/{empId}(empId=${employees[1].id})}">去 demo1 页面, 带 RESTful 风格参数

 

12. 全部标签

 

四、标签用法示例

https://www.w3xue.com/exp/article/20199/54847.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值