Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。

spring-boot-starter-thymeleaf

在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter即可:

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

在Spring Boot 1.5.9.RELEASE版本中,默认的Thymeleaf版本为2.1.6.RELEASE版本,这里推荐使用3.0以上版本。在pom中将Thymeleaf的版本修改为3.0.2.RELEASE:

<properties>
  <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  <thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
</properties>

在Spring Boot中,默认的html页面地址为src/main/resources/templates,默认的静态资源地址为src/main/resources/static。

Thymeleaf默认配置

在Spring Boot配置文件中可对Thymeleaf的默认配置进行修改:

#开启模板缓存(默认值:true)
spring.thymeleaf.cache=true
#Check that the template exists before rendering it.
spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
#Content-Type的值(默认值:text/html)
spring.thymeleaf.content-type=text/html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded-view-names=
#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
spring.thymeleaf.mode=HTML5
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
spring.thymeleaf.template-resolver-order=
#可解析的视图名称列表,用逗号分隔
spring.thymeleaf.view-names=

一般开发中将spring.thymeleaf.cache设置为false,其他保持默认值即可。

简单示例

编写一个简单的Controller:

@Controller
public class IndexController {

  @RequestMapping("/account")
  public String index(Model m) {
      List<Account> list = new ArrayList<Account>();
      list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));
      list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));
      list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));
      list.add(new Account("Maria", "玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));
      m.addAttribute("accountList",list);
      return "account";
  }
}

编写account.html页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>account</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" th:href="@{/css/style.css}" type="text/css">
</head>
<body>
  <table>
      <tr>
          <th>no</th>
          <th>account</th>
          <th>name</th>
          <th>password</th>
          <th>accountType</th>
          <th>tel</th>
      </tr>
      <tr th:each="list,stat : ${accountList}">
          <td th:text="${stat.count}"></td>
          <td th:text="${list.account}"></td>
          <td th:text="${list.name}"></td>
          <td th:text="${list.password}"></td>
          <td th:text="${list.accountType}"></td>
          <td th:text="${list.tel}"></td>
      </tr>
  </table>
</body>
</html>

最终项目目录如下所示:

启动项目,访问http://localhost:8080/web/account

source code

Spring Boot中使用thymeleaf的更多相关文章

  1. 在Spring MVC和Spring Boot中使用thymeleaf模板

    Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...

  2. Spring Boot中使用 Thymeleaf

    目录 1.pom.xml引入thymeleaf 2.关闭缓存application.properties 3.编写Controller类 4.模板html 5.运行结果 1.pom.xml引入thym ...

  3. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  4. spring boot 中的路径映射

    在spring boot中集成thymeleaf后,我们知道thymeleaf的默认的html的路径为classpath:/templates也就是resources/templates,那如何访问这 ...

  5. Spring Boot 中关于自定义异常处理的套路!

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

  6. Spring Boot 中的静态资源到底要放在哪里?

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...

  7. Spring Boot 中配置文件application.properties使用

    一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...

  8. Spring Boot中Starter是什么

    比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...

  9. Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅

    在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...

  10. Spring Boot中使用Spring Security进行安全控制

    我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache ...

随机推荐

  1. IBM服务器的2种IMM和1种raid管理方式

    IMM两种进入方式和Raid管理软件 前面板IMM管理接口:     用网线连接服务器前置面板的管理接口到其他 PC 或笔记本,然后 PC 或笔记本的 IP 地址配置为 192.168.70.0/24 ...

  2. linux系统过滤ip地址总结

    Perl模块用法 安装Perl模块 # 官网地址 https://metacpan.org/pod/Regexp::Common # 下载地址 https://cpan.metacpan.org/au ...

  3. centos7 双网卡同网段双网关配置

    需求: #1.服务器为双网卡: #2.网卡1为互联网 172.16.137.99/24/254 #3.网卡2为旅游专网 172.16.137.97/24/1 #4.互联网路由器为172.16.137. ...

  4. linux内核源码编译加制作rpm包

    本章主要讲解实际操作步骤,具体理论知识可以自行百度 linux内核官网下载:https://cdn.kernel.org/pub/linux/kernel/   (如图) 根据官网发布的信息分析,目前 ...

  5. git添加github和gitee多个git地址管理

    1.git init //初始化当前的git地址 2.git remote add github github.com //git remote add git标识 git地址 3.git pull ...

  6. CF1033E 题解

    题意 传送门 交互题,给定一个简单连通图,你可以询问一个点集 \(s\),返回其导出子图的边数.判断此图是否为二分图:若是,输出其中一部点的集合:否则输出任一个奇环.最多询问 \(20000\) 次. ...

  7. 爬取白鲸nft排名前25项目,持有nft大户地址数据。

    https://moby.gg/rankings?tab=Market SELECT address '钱包地址', COUNT (1) '持有nft项目数', SUM (balance) '持有nf ...

  8. 03 Spark RDD编程基础

    1. 准备文本文件从文件创建RDD lines=sc.textFile()筛选出含某个单词的行 lines.filter() 2. 生成单词的列表从列表创建RDD words=sc.paralleli ...

  9. Wps调用dll操作Excel表格转PDF

    起始原因:wps编辑创建的文档在microsoft office 中打开,会报内容存在异常是否恢复,因此wps文件被微软设定为破损文件,无法对原有文档进行操作运行,故在此使用wps对Excel进行操作 ...

  10. 谈谈 Redis 的过期策略

    在日常开发中,我们使用 Redis 存储 key 时通常会设置一个过期时间,但是 Redis 是怎么删除过期的 key,而且 Redis 是单线程的,删除 key 会不会造成阻塞.要搞清楚这些,就要了 ...