四、Spring Boot Web开发二之Thymeleaf

Spring Boot之Thymeleaf开发

1、Thymeleaf模板引擎

Spring Boot 提供了大量的模板引擎,包括FreeMarker\Groovy\Thymeleaf\Velocity和Mustache,Spring Boot 中推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的Spring MVC的支持。

2、Thymeleaf基础知识

Thymeleaf是一个Java类库,他说一个xml/xhtml/html5的模板引擎,可以作为MVC的web应用的View层。
Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们还可以使用Thymeleaf完全替代JSP。

2.1、引入Thymeleaf

在标签中添加thymeleaf命名空间,将静态页面转换为动态的视图。
通过”@{}”引用web静态资源。

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script th:src="@{jqeury.min.js}" type="text/javascript"></script>
</body>
</html>

2.2范围model中的数据

通过”${}”访问model中的属性,这个jsp即为相似。

<div>
<h3>访问model</h3>
<span th:text="${singlePerson.name}"></span>
</div>

2.3.引入URL

Thymeleaf对于URL的处理是通过语法@{…}来处理的

<a th:href="@{https://blog.csdn.net/java_mdzy/article/category/7716138}">绝对路径</a>
<a th:href="@{/}">相对路径</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值