Thymeleaf模板引擎

一、IDEA中SpringBoot+Thymeleaf配置

1、pom添加以下依赖
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、html文件配置

开始idea中html直接用thymeleaf表达式,会出现红线,这时你可以用Alt+Enter,html会引入<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >

3、使用注意

需要将文件放入目录:“classpath:/templates/”;
文件的后缀:".html";

二、Thymeleaf语法

1、注释

单行注释:!-- 你看不到我~~~~~ -->
多行注释:

<!--/*
你看不到我~~~
*/-->
2.1、使用前缀th

Thymeleaf使用属性都需加前缀th,如<span th:text="wswdwf"></span>

2.2、th作用

th就是替换原有html的值:th:html属性名=值;

Controller代码
在这里插入图片描述
home.html文件中的代码在这里插入图片描述
网页中输出的结果
在这里插入图片描述

3、读取变量

${变量名},

<div th:text="${color}"></div>
4、迭代循环

th:each

<div th:each="color:${colors}">
<div th:text="${color}"></div>
</div>
5、text和utext的区别

Controller代码
在这里插入图片描述

th:text :

语法:
<p th:text="${welcome}"></p>

若传进的变量带有html标签,不会被解析
结果为
在这里插入图片描述

th:utext :

语法:
<p th:utext="${welcome}"></p>

用utext,会解析html,显示相应的效果
结果为
在这里插入图片描述

小结

用th:text不会解析html,用th:utext会解析html,在页面中显示相应的样式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值