一、Thymeleaf

1.1 集成 templates

pom.xml 文件中添加依赖

<dependencies>
......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

application.yml 文件中添加配置

spring:
thymeleaf:
# 模板文件前缀
prefix: classpath:/templates/
# 模板文件后缀
suffix: .html

​ 注:这些属性是 ThymeleafProperties 默认的配置,如果不需要变更的话可以不用配置。

1.2 实例

java目录下新建 HelloController.java 文件

@Controller
public class HelloController { @RequestMapping("/hello")
public String hello(Model model) {
// 普通文本
model.addAttribute("text","MarkLogZhu");
// 普通类型数组
int[] array = new int []{10,2,33,4,5};
model.addAttribute("array", array);
// 对象列表
List<User> users = new ArrayList<>();
users.add(new User(1,"张三"));
users.add(new User(2,"李四"));
users.add(new User(3,"王五"));
model.addAttribute("users", users);
return "hello";
} }

templates 目录下新建 hello.html 文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>SpringBoot-thymeleaf demo</title>
</head>
<body>
<p th:text="'hello, ' + ${text} + '!'"/> <table>
<tr th:each=" arr : ${array}">
<td th:text="${arr}">序号</td>
</tr>
</table> <table>
<thead>
<th>序号</th>
<th>用户 id</th>
<th>用户名</th>
</thead>
<tbody>
<tr th:each=" user,userStat : ${users}">
<td th:text="${userStat.index+1}">序号</td>
<td th:text="${user.userId}">用户 id</td>
<td th:text="${user.userName}">用户名</td>
</tr>
</tbody>
</table> </body>
</html>

启动项目,在浏览器访问:http://localhost:8080/hello

1.3 常用语法

关键字 功能介绍
th:id 替换id
th:text 文本替换
th:utext 支持html的文本替换
th:object 替换对象
th:value 属性赋值
th:with 变量赋值运算
th:style 设置样式
th:onclick 点击事件
th:each 属性赋值
th:if 判断条件
th:unless 和th:if判断相反
th:href 链接地址
th:switch 多路选择 配合th:case 使用
th:case th:switch的一个分支
th:fragment 布局标签,定义一个代码片段,方便其它地方引用
th:include 布局标签,替换内容到引入的文件
th:replace 布局标签,替换整个标签到引入的文件
th:selected selected选择框 选中
th:src 图片类地址引入
th:inline 定义js脚本可以使用变量
th:action 表单提交的地址
th:remove 删除某个属性
th:attr 设置标签属性,多个属性可以用逗号分隔

更多语法请参考 thymeleaf 官方文档

SpringBoot之模板引擎的更多相关文章

  1. springboot集成模板引擎freemarker和thymeleaf

    freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...

  2. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器

    目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...

  3. SpringBoot系统列 5 - 接口版本控制、SpringBoot FreeMarker模板引擎

    接着上篇博客的代码继续写 1.接口版本控制 一个系统上线后会不断迭代更新,需求也会不断变化,有可能接口的参数也会发生变化,如果在原有的参数上直接修改,可能会影响线上系统的正常运行,这时我们就需要设置不 ...

  4. springboot:Java模板引擎Thymeleaf介绍

    Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用 ...

  5. SpringBoot整合模板引擎

    一.SpringBoot整合freemarker: 1.引入freemarker模板依赖: <dependency> <groupId>org.springframework. ...

  6. JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎

    上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...

  7. Springboot与Thymeleaf模板引擎整合基础教程(附源码)

    前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...

  8. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  9. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

随机推荐

  1. opengl入门篇一: 第一个三角形

    话说程序员有三大浪漫,操作系统.编译原理和计算机图形学.这里称作计算机图形学,而不是图形学,是为了避免歧义. opengl是干什么的,可以自行google.这里仅作为一个学习里程中的记录.不作为权威指 ...

  2. Unity中的动画系统和Timeline(3) 混合树和动画匹配

    混合树 前面我们通过在Animation界面添加单独的动作来控制动画,这样做比较麻烦,每个单独的属性,比如站立,奔跑等,都需要单独的代码来控制.现在我们可以通过使用混合树,其基本思想是将相近的动画混合 ...

  3. LeetCode.1033-移动石头直到连续(Moving Stones Until Consecutive)

    这是小川的第386次更新,第414篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第247题(顺位题号是1033).在a,b和c位置的数字线上有三块石头.每次,你在一个终点 ...

  4. [Vuejs] Vue解决代理设置导致浏览器Session丢失的问题

    以下是会造成Session丢失的代理: proxyTable: { '/proxyApi': { target: 'http://111.18.149.215:8080/WRSOCXSystem/ht ...

  5. yum源迁移(思路具体操作之后加)

    准备工作,有一台能联网的机器装有liunx系统 首先在联网机器下载yum系列包(yum命令如果不存在的话只能通过安装包的形式进行安装这里不考虑yum命令不存在情况) 修改配置文件使得yum命令只下载不 ...

  6. 微信小程序--catchtap&bindtap

    转自:https://www.cnblogs.com/heron-yu/p/7244481.html 转自:http://blog.csdn.net/xiaochun365/article/detai ...

  7. js if(!!!e) {} 判断条件中的三个感叹号什么意思

    两个感叹号的意思就是,将变量转换为其对应的布尔值. !!e就是e对应的布尔值,true或者false. !!!e==!(!!e)==!true/!false=false/true;

  8. python 并发编程 多线程 目录

    线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...

  9. Spring Gateway从入门到精通

    1.Spring Gateway过滤器详解 2.Spring Gateway之Predicate详解 3.spring cloud gateway自定义过滤器 4.Spring Cloud Gatew ...

  10. Linux上面执行 Windows 命令(比如 重启服务)的简单方法

    1. 首先 基础是:openssh 还有 expect的包 2. 方法 安装openssh 转帖来自: https://www.jianshu.com/p/6e5bc39d386e 最近项目在搞Jen ...