Thymeleaf

Thymeleaf


什么是Thymeleaf

Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。

模板引擎

使用户界面与业务数据分离而产生的,它可以生成特定的文档,用于网站的模板引擎就会生成一个标准的html文档。
比如教室,在学生和老师到来之前,把桌椅讲台布置好,老师同学来了之后直接使用即可。

特点

动静分离:使用html通过一些特定标签语法代表其含义,但并未破坏html结构,即使无网络、不通过后端渲染也能在浏览器成功打开,方便界面的测试和修改。

传统的jsp模板引擎,本质上是将html文件修改后缀为.jsp,然后在这个文件中增加自己的语法、标签,执行时通过后台处理这个文件最终返回一个html页面。浏览器无法直接识别.jsp文件,需要借助网络(服务器)才能访问。

Thymeleaf用html做模板可以直接在浏览器中打开。开发者将Thymeleaf的语法通过html的标签属性来定义完成,这些标签属性不会影响html页面的完整性和显示,如果通过后台服务端访问页面服务端会寻找这些标签将服务端对应的数据替换到相应位置实现动态页面。
在这里插入图片描述

动态页面每次修改都需要重新启动程序、输入链接。但是如果界面设计人员通过静态页面设计样式,设计完成通过服务端访问即可达到目标UI的界面和应用,达到动静分离的效果。

开箱即用:直接套用模板实现JSTL、OGNL表达式的效果,避免套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

thymeleaf配置说明

# 是否允许页面缓存,在开发过程中需要确保页面是最新的所以需要禁用缓存
# 而在上线运营时,页面不常改动,为了减少服务端压力以及提升客户端响应速度会允许使用页面缓存
spring.thymeleaf.cache = false

# 确定页面的编码格式
spring.thymeleaf.encoding = UTF-8

# springboot默认模板引擎文件放在templates下
spring.thymeleaf.prefix = classpath:/templates/

常用标签

标签作用示例
th:id替换id<input th:id="${user.id}">
th:text文本替换<p th:text="${user.name}">Here is an example</p>
th:utext支持html的文本替换<p th:utext="${htmlcontent}">content</p>
th:object替换对象<div th:object="${user}"></div>
th:value替换值<input th:value="${user.name}">
th:each迭代<tr th:each=“student:${user}”>
th:href替换超链接<a th:href="@{index.html}">超链接 </a>
th:src替换资源<script type=“text/javascript” th:src="@{index.js}"></script>

链接表达式

引入thymeleaf模板引擎

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

引入css

<link rel="stylesheet" th:href="@{index.css}">

引入JavaScript

<script type="text/javascript" th:src="@{index.js}"></script>

超链接

<a th:href="@{index.html}">超链接</a>

变量表达式:${…}

urlController.java

@Controller
public class urlController {

    @GetMapping("index")//页面的url地址
    public String getIndex(Model model)//对应函数
    {
        // JavaBean
        User user=new User("zhangSan",22,"一个幽默且热爱java的社会青年");

        //List
        List<String> userList=new ArrayList<>();
        userList.add("Learning");
        userList.add(" requires");
        userList.add(" persistence !");

        //Map
        Map<String ,String> map=new HashMap<>();
        map.put("place","beijing");
        map.put("time","12:00");

        //数据添加到model中
        model.addAttribute("name","this is a normal string");//普通字符串
        model.addAttribute("user",user);//储存javabean
        model.addAttribute("userList",userList);//储存List
        model.addAttribute("map",map);//储存Map

        return "index";//与templates中index.html对应
    }
}

index.html

<!DOCTYPE html>
<html lang="en">
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>第一个Thymeleaf程序</title>
</head>
<body>
    <!-- name,普通字符串 -->
    <div th:text="${name}">name:示例</div><br>

    <!-- JavaBean -->
    <div th:text="'name:'+ ${user.getName()}+
    			  ',age:'+ ${user.getAge()}+
    			  ',detail:'+ ${user.getDetail()}
    			  "></div><br>
	
    <!-- 遍历List -->
    <span th:each="item:${userList}">
        <span th:text="${item}"></span>
    </span><br><br>

    <!-- 遍历Map -->
    <div th:each="item:${map}">
        <span th:text="${item.key}+''"></span>
        <span th:text="${item.value}"></span><br>
    </div>
    
    <!-- 通过map中的key获取对应的value -->
    <span th:text="'place:'+${map.get('place')}+''"></span>
    <span th:text="'time:'+${map.get('time')}"></span><br>

</body>
</html>

结果:
在这里插入图片描述

消息表达:#{…}

文本外部化是从模板文件中提取模板代码的片段,以便可以将它们保存在单独的文件通常是.properties文件,文本的外部化片段称为“消息”。即用#{…}语法来读取配置文件中的数据。

application.properties

spring.messages.basename=templates/home

templates目录下的home.properties文件

xiaoMing.name = xiaoMing
xiaoMing.age = 21

index.html中使用

    <div th:text="#{xiaoMing.name}"></div>
    <div th:text="#{xiaoMing.age}"></div>

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

多少梦境难成真

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值