SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎
SpringBoot 学习笔记 Part12
1. thymeleaf简介
SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。
Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.
即 thymeleaf 是现代化、服务端的Java模板引擎。
2. 基本语法
表达式
表达式名 语法 作用 变量取值 ${…} 获取请求域、session域、对象等值 选择变量 *{…} 获取上下文对象值 消息 #{…} 获取国际化等值 链接 @{…} 生成链接 片段表达式 ~{…} jsp:include 作用,引入公共页面片段 行内表达式 [[…]] HTML 文本中嵌套表达式 字面量
文本值:‘one text’ , ‘another one’
数字:0 , 34 , 3.0 , 12.3
布尔值:true , false
空值:null
变量:one,two
文本操作
字符串拼接: +
变量替换: |The name is ${name}|
数字运算
运算符: + , - , * , / , %
布尔运算
运算符: and , or
一元运算: ! , not
比较运算
比较: > , < , >= , <= ( gt , lt , ge , le )
等式: == , != ( eq , ne )
条件运算
If-then: (if) ?(then)
If-then-else: (if) ? (then) :(else)
Default: (value) ? :(defaultvalue)
特殊操作
无操作: _
3. 标签th:attr
th:attr 用来设置属性值。
- 设置单个值:
<form action="subscribe.html" th:attr="action=@{/subscribe}"><fieldset><input type="text" name="email" /><input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/></fieldset>
</form>
- 设置多个值:
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
- 以上两个的代替写法:
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
所有h5兼容的标签写法可参考官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes
4. 迭代
foreach:
- 迭代普通类型:
<tr th:each="prod : ${prods}"><td th:text="${prod.name}">Onions</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
- 迭代对象:
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'"><td th:text="${prod.name}">Onions</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
5. 条件运算
if:
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
switch:
<div th:switch="${user.role}"><p th:case="'admin'">User is an administrator</p><p th:case="#{roles.manager}">User is a manager</p><p th:case="*">User is some other thing</p>
</div>
6. 属性优先级
所有Thymeleaf属性都有一个数字优先级,以建立他们在标签中的顺序执行。这个顺序是:
Order | Feature | Attributes |
---|---|---|
1 | Fragment inclusion |
th:include th:replace
|
2 | Fragment iteration |
th:each
|
3 | Conditional evaluation |
th:if th:unless th:switch th:case
|
4 | Local variable definition |
th:object th:with
|
5 | General attribute modification |
th:attr th:attrprepend th:attrappend
|
6 | Specific attribute modification |
th:value , th:href , th:src , etc.
|
7 | Text (tag body modification) |
th:text th:utext
|
8 | Fragment specification |
th:fragment
|
9 | Fragment removal |
th:remove
|
7. thymeleaf的使用
首先,我们需要引入thymeleaf的starter场景启动器。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
我们可以在 springboot 中找到 thymeleaf 的自动配置类,这说明springboot已经为我们做好了默认配置的工作。
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }
查看 springboot 的 thymeleaf 的自动配置类,在自动配置类里我们可以找到一些已经配好的重要组件,如spring模板引擎 SpringTemplateEngine、thymeleaf 的视图解析器 ThymeleafViewResolver 等。并且我们发现所有thymeleaf的配置值都在 ThymeleafProperties 类里,这个是存储配置信息的配置类,之前学习过。
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html"; //xxx.html
ThymeleafProperties 类中这两个重要属性表明了我们开发的页面路径和映射规则,因此我们不需要别的配置,直接进行开发就行了。
注:在html页面开发中为了更方便地使用thymeleaf标签属性,应在标签中引入thymeleaf的命名空间 xmlns:th=“http://www.thymeleaf.org”,如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body></body>
</html>
8. 开发小案例
h5中编写如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1 th:text="${msg}">Hello H5 !</h1>
<h2><a href="www.4399.com" th:href="${link}">去百度</a>
</h2></body>
</html>
Controller中编写如下:
@Controller
public class thymeleafController {@GetMapping("/thymeleaf")public String gotoThymeleaf(Model model){model.addAttribute("msg","Hello Thymelef !");model.addAttribute("link","http://www.baidu.com");return "success";}
}
SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎相关推荐
- Springboot学习笔记(二)Web开发
前言: 学习B站UP主狂神说视频笔记整理视频链接 狂神笔记链接 上篇笔记链接-Springboot学习笔记(一)快速上手 Web开发 静态资源 在以往的SpringMVC中所有静态资源或者页面应该放在 ...
- Python学习笔记:web开发3
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- Python学习笔记:web开发2
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- Python学习笔记:web开发1
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- 从零开始学 Python 之 Web 开发 Jinja2 模板引擎
被之前的文章中,简单介绍了 Python Web 开发框架 Flask,知道了如何写个 Hello World,但是距离用 Flask 开发真正的项目,还有段距离,现在我们目标更靠近一些 -- 学习下 ...
- Spring Boot 2 精髓学习笔记(五)---Beetl 模板引擎(1)
本节介绍MVC 中的视图技术之模板引擎Beetl ,用于渲染模板. 一.Beetl模板引擎 Beetl是2010 年开发井维护至今的一个模板引擎,具有如下特点: 功能完备.作为主流模板引擎,适用于各种 ...
- Python学习笔记:Day1-2 开发环境搭建
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
最新文章
- KDD 2021 即将开幕!当线上 AI 顶会开始内卷.....
- 全国计算机等级考试二级笔试样卷Java语言程序设计
- PAT (Basic Level) Practice (中文)C++ python 语言实现 —— 题解目录
- phpcms发布新闻到数据库_如何利用PHPCMS调用新闻列表
- java中立方根方法_java基础:4、java中Math类的常用方法?
- Vue工程模板文件 webpack打包
- vue项目中更新element-ui版本
- Snmp网络协议及Java开发相关
- [转载] C语言C++指针与java中引用的一点对此
- 智能优化算法(源码)-飞蛾扑火优化算法(MFO)
- Java实现十字链表
- SCSI代码分析(2)SCSI设备的管理1
- 问题记录1:网络和共享中心打不开的解决办法
- 蓝桥杯——第九届C语言B组第五题:x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机;
- tomcat配置https(jks)证书,启动后提示Keystore was tampered with, or password was incorrect
- 华为电脑计算机怎么显示在桌面,电脑桌面小便签,华为电脑怎么设置桌面便签...
- Circular reasoning
- c语言 取余 % 和除法 / 的应用技巧 (在取位数方面的)
- 使用Hbuilder开发python
- 53道常见NodeJS基础面试题(附答案)