spring boot模板引擎thymleaf用法详解

Spring-boot支持FreeMarker、Thymeleaf、jsp、veocity
但是对freemarker和thymeleaf的支持最好,不推荐使用jsp

使用jsp的弊端

1:项目目录结构繁琐

2:页面不简洁

3:jsp内置错误页面不能覆盖springboot默认的错误页面

4: 只能打成war不能打成jar

5:内置的jetty服务器不支持jsp

thymeleaf(新一代模版引擎)

优点:

1:有网无网的情况下模版页面都可以执行,美工的页面拿来就可以用.

2:相对jsp减少了额外的标签,页面也更加简洁

jar包依赖

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

在application.properties中配置thymleaf

spring.thymeleaf.mode = LEGACYHTML5

spring.thymeleaf.mode的默认值是HTML5,其实是一个很严格的检查,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。

自定义视图解析

spring.thymeleaf.prefix=classpath:/templates/html/ 不要漏写
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false

访问静态资源

<img src=”/images/….”>
<script src=”/js/….”>
<link href=”/css/….”>

访问map中的数据

@RequestMapping("/index")
public  String hello(Model map){//mapMap<String, Object> student= new HashMap<>();student.put("name", "张三丰");map.addAttribute("student",student);return "index";
}

在HTML页面取数据

<span th:text="${student.name}"></span>
字符串拼接:<h2 th:text="'姓名:'+${student.name}"></h2>
三元表达式:<input th:value="${age gt 30 ? '中年':'年轻'}"/>
gt:great than(大于)
ge:great equal(大于等于)
eq:equal(等于)
lt:less than(小于)
le:less equal(小于等于)
ne:not equal(不等于)

访问pojo中的属性

Book book = new Book("辟邪剑谱",199.99f,"http://img3m6.ddimg.cn/15/16/23326296-1_w_2.jpg");
map.addAttribute("book",book);

在HTML页面取值

<img th:src="${book.bookUrl}"/><span th:text="${book.bookName}"/>

取list中的数据

List<Book> books = new ArrayList<Book>();
for (int i = 0; i < 10; i++) {Book b = new Book("book"+i, 100f, "http://www.wendaoxueyuan.com/images/"+i+".jpg");books.add(b);
}
map.addAttribute("books",books);

HTML页面取值

<table border="1px" cellspacing="0px" cellspadding="0px" width="100%"><tr><td>编号</td><td>书名</td><td>书价格</td><td>图片地址</td></tr><tr th:each="book:${books}"><td>编号</td><td th:text="${book.bookName}">书名</td><td th:text="${book.bookPrice}">书价格</td><td th:text="${book.bookUrl}">图片地址</td></tr>
</table>

取循环中的下标

<tr  th:each="user,userStat : ${list}">   <th th:text="${userStat.index}">状态变量:index</th>  <th th:text="${userStat.count}">状态变量:count</th>  <th th:text="${userStat.size}">状态变量:size</th>  <th th:text="${userStat.current.userName}">状态变量:current</th>  <th th:text="${userStat.even}">状态变量:even****</th>  <th th:text="${userStat.odd}">状态变量:odd</th>  <th th:text="${userStat.first}">状态变量:first</th>  <th th:text="${userStat.last}">状态变量:last</th>
</tr> 说明:
index:列表状态的序号,从0开始;
count:列表状态的序号,从1开始;
size:列表状态,列表数据条数;
current:列表状态,当前数据对象
even:列表状态,是否为奇数,boolean类型
odd:列表状态,是否为偶数,boolean类型
first:列表状态,是否为第一条,boolean类型
last:列表状态,是否为最后一条,boolean类型

利用下标实现表格变色

<tr th:class="${pcStatus.even?'red':'blue'}">

if判断

<h1><b th:text="${name}"></b>:<span th:if="${age gt 30}">中年</span><span th:unless="${age gt 30}">年轻</span>
</h1>

将pojo中的Date类型数据渲染成String

<span th:text="${#dates.format(post.postCreate,'yyyy-MM-dd')}">2017年11月8日</span>

定义和引用片段

定义

<div th:fragment="copy">&copy; 2014 The Good Thymes Virtual Grocery</div>

引用

<body>...<div th:include="footer :: copy"></div>
</body>

spring boot模板引擎thymleaf用法详解相关推荐

  1. Spring Boot的每个模块包详解

    Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...

  2. spring boot(四):thymeleaf使用详解

    spring boot(四):thymeleaf使用详解 在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用. ...

  3. Spring Boot 模板引擎FreeMarker集成

    Spring Boot 模板引擎FreeMarker集成 一.FreeMaker介绍 FreeMarker是一款免费的Java模板引擎,是一种基于模板和数据生成文本(HMLT.电子邮件.配置文件.源代 ...

  4. Spring Boot 使用 Druid 连接池详解

    Spring Boot 使用 Druid 连接池详解 Alibaba Druid 是一个 JDBC 组件库,包含数据库连接池.SQL Parser 等组件,被大量业务和技术产品使用或集成,经历过严苛的 ...

  5. Spring Boot 2.0 的配置详解(图文教程)

    本文来自作者 泥瓦匠 @ bysocket.com 在 GitChat 上分享 「Spring Boot 2.0 的配置详解(图文教程)」 编辑 | 哈比 Spring Boot 配置,包括自动配置和 ...

  6. Spring Boot 配置加载顺序详解

    使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...

  7. (转)Spring Boot(四):Thymeleaf 使用详解

    http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html 在上篇文章Spring Boot (二):Web 综 ...

  8. Spring boot模板引擎缓存

    模板引擎缓存 模板引擎,默认开启缓存 开发的时候,需要关闭模板引擎的缓存 在application.properties中配置 spring.thymeleaf.cache=false 否则,修改HT ...

  9. Spring boot模板引擎

    Thymeleaf SpringBoot推荐Thymeleaf模板引擎 语法更简单,功能更强大 引入thymeleaf <dependency><groupId>org.spr ...

最新文章

  1. 如何通过报表单元格右键控制报表跳转到不同链接地址
  2. python 之模块引入
  3. 取生产订单状态的逻辑
  4. 【MongoDB】Sharding分片概念及原理
  5. 如何在 SAP BTP 上 手动执行 workflow
  6. 记录我的学习历程--二维数组解决平面图形题
  7. SDK 操作 list-view control 实例 -- 遍历进程
  8. 【POJ - 3253】Fence Repair(贪心,时光倒流)
  9. android学习笔记---62_自定义窗口标题
  10. Sublime Text4添加配色主题
  11. bootstrap - 弹出层
  12. PS快捷键总结,(操作、设置)小技巧
  13. CorelDRAW暗角效果怎么做?
  14. 2020年阴历二月二十六 投资理财~读万科财报有感
  15. 微信内网页分享,分享者能看到分享的图片(描述),但被分享者无法看到
  16. channel java_Java Channel
  17. ROS2编程基础课程--Launch
  18. Visual Studio Code(VSCODE)修改字体、字号
  19. win8找到程序员计算器
  20. 合格站长必须知道的5点常规知识

热门文章

  1. 【Python基础】Github标星4.7k,每天推送一个python小实例的Python库
  2. 【机器学习基础】数学推导+纯Python实现机器学习算法27:LDA线性判别分析
  3. 机器学习新论文推荐-(成对关系约束的非负矩阵分解)
  4. 产品经理和UX设计师,变革已可期
  5. 猴年如何抢红包?错过这秘籍可能错过几个亿!
  6. 文青工程师陈盛 — 程序员+文青是怎样一种体验
  7. js获取时间段内属于星期一的日期们
  8. 新后缀勒索病毒.phobos 解密成功 sql数据恢复
  9. ajax post 表单和 json 字符串
  10. Python性能优化的20条建议