thymeleaf使用、运行、简单语法

        主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易协作。Thymeleaf能够处理HTML,xml,Js,css甚至纯文本。

Thymeleaf是原生的,不依赖于标签库。它能够在接受原始HTML的地方进行编辑和渲染。因为它没有与Servlet规范耦合,因此Thymeleaf模板能进入jsp所无法涉及的领域。

文档支持和页面原型支持;没法把jsp改成html,所以有了thymeleaf;所以产品经理所制定的页面后端程序员很难进行复用;画好的原型页面thymeleaf能够直接使用;所以有了模板引擎,thymeleaf>freemarker;【这是单体项目特点】

【分布式的,前后端分离的】thymeleaf就没有特点了;

××××××××××××××××××××××××基本使用thymeleaf

它的html页面不允许直接访问,需要浏览器进行渲染;

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">

 

在运行时,springboot的自动配置会发现在Thymeleaf在类路径中,因为会为SpringMVC创建支撑Thymeleaf视图的bean。

像Thymeleaf这样的视图库在设计时与特定的web框架解耦的。这样的话它们就无法感知Spring的模型对象,因此无法与控制器放到Model中的数据协同工作。但是它可以与Servlet的request属性进行协作。所以,在Spring将请求转换到视图之前,它会把模型数据复制到request属性中,Thymeleaf和其他的视图模板就能够访问到它们了。

文本显示


<p th:text="${message}" > placeholder message  </p>

循环

<div th:each="ingredient : ${wrap}">
    <input name="gredients" type="checkbox" th:value="${ingredient.id}"/>
    <span th:text="{ingredient.name}">Ingredient</span><br/>
</div>

链接

<link rel="stylesheet" th:href="@{/style.css}" />
<img  th:src="@{/images/thymeleaf01.png}"/>

表单

<form method="post" th:object="${design}">

<span th:text=”pshd”>hx</span>

<span th:”${msg}”> 从controller来

输出:pshd,标签中的内容会被替换

输出了变量的值;

Th:value

可将一个值放入到input的标签的value中

内置对象:可以在模板中直接使用,用#

 

${#strings.isEmpth(key)}

判断字符串是否为空ture/false

${#strings.contains(msg,’T’)}

判断字符串是够包含指定字符串

${strings.strtsWith(msg,’a’)}

判断当前串是否以子串开头

${#strings.length(msg)}

返回字符串长度

${#strings.indexOf(msg,’h’)}

查找子串的位置,返回其下标/-1

${#strings.substring(msg,2)}

${#strings.substring(msg,2,5)}

 

${#dates.format(key)}

格式化日期为浏览器默认格式标准

${#dates.format(key,’yyyy/MM/dd’)}

${#dates.year(key)}

${#dates.month(key)}

${#dates.day(key)}

 

th:if

条件判断

th:switch/th:case

多个匹配,显示第一条配置;没有default,但是th:case=”*”等同

Th:each

状态变量:

Index:当前迭代器的索引从0开始

Count:当前迭代对象的计数从1开始

Size:被迭代对象的长度

Odd/even:布尔值,当前循环是否是偶数/奇数 从0开始

First:布尔值,当前循环的是否是第一条

Last:布尔值,当前逊汗的是否是最后一条

迭代器,用户循环迭代集合

 

 

 

<span th:text=”${#strings.isEmpty(msg)}”></span>
Model.addAttribute(“sex”,”男”);
<div>
<span th:if=”${sex}==’男’”>
性别:男
</span>
<span th:if=”${sex}==’女’”>
性别:女
</span>
</div>

<div th:switch=”${id}”>
<span th:case=”1”>
ID为1的内容
</span>
</div>


<table border=”1” >
<tr>
<th>ID</th>
<th>Name</th>
<th>age</th>
</tr>
<tr th:each=”u : ${lisst}”>
<td th:text=”${u.id}”></td>
<td th:text=”${u.Name}”></td>
<td th:text=”${u.age}”></td>
</tr>
</table>
Th:each 迭代Map
<table>
<tr>
<th>Value</th>
</tr>
<tr th:each=”m : ${map}”>
<td th:text=”${m}”></td>     输出的是对象的地址是entry类型
</tr>
<tr th:each=”m :${map}”>
<td th:text=”${m.value.id}”></td>
<td th:text=”${m.value.name}”></td>
<td th:text=”${m.value.age}”></td> 
</tr>
</table>


×××××××××××××××××××操作域对象
Request.setAttribute(“req”,”HttpServletRequest”);
Request.getSesson().setAttribute(“ses”,”httpSession”);
Request.getSession().getServletContext().setAttribute(“app”,”application”)


HttpServletRequest
Request.setAttribute(“req”,”HttpServletRequest”);
<span th:text=”${#httpServletRequest.getAttribute(‘req’)}”></span>
<span th:text=”${#request.getAttribute(‘req’)}”></span>

HttpSession
Request.getSession.setAttribute(“ses”,”HttpSession”);
<span th:text=”${#session.sess}”></span><br/>
<span th:text=”${#session.getAttribute(‘ses’)}”></span>

springboot支持模板的方案

模板springboot的starter的依赖
FreeMarkerspring-boot-starter-freemarker
Groovy Templatesspring-boot-starter-groovy-templates
java server page由tomcat和jetty提供
Mustachespring-boot-starter-mustache
Thymeleafspring-boot-starter-thymeleaf

 

缓存模板

默认情况下,模板只有在第一次使用的时候解析一次,解析的结果会被后续的请求所使用。对于生产环境来说,这是一个很棒的特性,它能防止每次请求时多余的模板解析过程,因此有助于提升性能。但是,在开发期就太不好了,无法看到页面的修改效果。因此有一种方法可以禁用缓存。我们只需在application.properties中添加spring.thymeleaf.cache=false。唯一需要注意的是,在将应用部署到生产环境之前,这一行代码一定要删除。另一种更简单的方式是单独配置一个测试环境的profile,打包时改成正式环境的配置文件即可。

模板启用缓存的属性
FreeMarkerspring.freemarker.cache
Groovy Templatesspring.groovu.template.chche
Mustachespring.mustache.cache
Thymeleafspring.thymeleaf.chche

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单使用Thymeleaf模板引擎的Spring Boot示例: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 创建模板文件 在 src/main/resources/templates 目录下创建一个 HTML 文件,例如:index.html。在该文件中使用 Thymeleaf 模板语法。 例如: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Demo</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 在上面的代码中,${message} 表示使用 Thymeleaf 的变量表达式,它会被替换为 Controller 中设置的 message 值。 3. 创建 Controller 创建一个 Spring Boot 控制器类,并在其中设置模板所需的数据。 例如: ```java @Controller public class DemoController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 在上述代码中,使用 @Controller 注解表示这是一个控制器类,使用 @GetMapping("/") 注解表示访问根路径时,调用 index 方法。在 index 方法中,使用 Model 对象将 message 值设置为 "Hello, Thymeleaf!"。 4. 运行应用程序 运行应用程序,然后在浏览器中访问 http://localhost:8080/,你应该可以看到一个包含 "Hello, Thymeleaf!" 文本的页面。 以上就是一个简单的 Spring Boot 案例使用 Thymeleaf 模板引擎。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值