1.Thymeleaf概述

SpringBoot并不推荐使用jsp,但是支持一些模板引擎技术:Freemarker、Thymeleaf、Mustache。

简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较于其他的模板引擎,它有如下四个极吸引人的特点:
  - 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
  - 开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  - 多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
  - 与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

2.Thymeleaf快速入门

(1)引入Thymeleaf的依赖

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

(2)编写一个controller方法,返回一些用户数据,放入模型中,将来在页面渲染

package lucky.controller;

import lucky.domain.Users;
import lucky.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
@RequestMapping(path = "/users")
public class UsersController { @Autowired
private UsersService usersService; @RequestMapping(path = "/query")
@ResponseBody
public String queryUsers(){
return "hello users";
} @RequestMapping(path = "/queryUsersById")
@ResponseBody
public Users queryUsersById(@RequestParam("id") Integer id){
return this.usersService.queryUsersById(id);
} /**
* 查询所有用户,并在前端显示
* @param model model对象用来向前端传递数据
* @return 返回视图名称
*/
@RequestMapping(path = "/queryAllUsers")
public String queryAllUsers(Model model){
//1.查询所有用户
List<Users> users = this.usersService.queryAllUsers();
//2.放入模型
model.addAttribute("users",users);
//3.返回模板名称(就是classpath:/templates/目录下的html文件名)
return "users";
} }

(3)静态页面

Thymeleaf会根据前缀和后缀来确定模板文件的位置。分析源码可得:

- 默认前缀:classpath:/templates/
- 默认后缀:.html

如果我们返回视图:users,会指向到 classpath:/templates/users.html

根据上面的源码分析,模板默认放在classpath下的templates文件夹,我们新建一个html文件放入其中:

编写html模板,渲染模型中的数据:

注意,把html 的名称空间,改成:xmlns:th="http://www.thymeleaf.org"会有语法提示

我们看到这里使用了以下语法:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
table, th, td {border: 1px solid darkslategray;padding: 10px}
</style>
</head>
<body>
<div style="text-align: center">
<span style="color: darkslategray; font-size: 30px">欢迎光临!</span>
<hr/>
<table class="list">
<tr>
<th>id</th>
<th>姓名</th>
<th>密码</th>
<th>中文名</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}">1</td>
<td th:text="${user.name}">张三</td>
<td th:text="${user.password}">lucky</td>
<td th:text="${user.name}">zhangsan</td>
</tr>
</table>
</div>
</body>
</html>

我们看到这里使用了以下语法:

- ${}:这个类似与el表达式,但其实是ognl的语法,比el表达式更加强大
- th-指令:th-是利用了Html5中的自定义属性来实现的。如果不支持H5,可以用`data-th-`来代替
- th:each:类似于c:foreach遍历集合,但是语法更加简洁
- th:text:声明标签中的文本
- 例如<td th-text='${user.id}'>1</td>,如果user.id有值,会覆盖默认的1
- 如果没有值,则会显示td中默认的1。这正是thymeleaf能够动静结合的原因,模板解析失败不影响页面的显示效果,因为会显示默认值!

3.效果图

004 Thymeleaf学习笔记的更多相关文章

  1. Thymeleaf 学习笔记-实例demo(中文教程)

    项目demo     http://pan.baidu.com/s/1wg6PC 学习资料网址  http://www.blogjava.net/bjwulin/archive/2013/02/07/ ...

  2. thymeleaf学习笔记

    1.${@dict.hello().fatherName} 显示对象的属性2.${@dict.hello()[0].fatherName} 显示列表对象的属性3.<div th:object=& ...

  3. thymeleaf学习笔记:总结

    Thymeleaf定义:Thymeleaf is a modern server-side Java template engine for both web and standalone envir ...

  4. Thymeleaf 学习笔记

    (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引 ...

  5. thymeleaf 学习笔记-基础篇(中文教程)

    (一)Thymeleaf 是个什么?      简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...

  6. thymeleaf 学习笔记(转)

    原文:http://blog.csdn.net/pdw2009/article/details/44410659 thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker ...

  7. C语言学习笔记之成员数组和指针

    成员数组和指针是我们c语言中一个非常重要的知识点,记得以前在大学时老师一直要我们做这类的练习了,但是最的还是忘记了,今天来恶补一下.     单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个 ...

  8. Web前端学习笔记(001)

    ....编号    ........类别    ............条目  ................明细....................时间 一.Web前端学习笔记         ...

  9. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

随机推荐

  1. UFUN函数 UF_CURVE函数(UF_CURVE_create_line、UF_CURVE_create_arc、UF_CURVE_ask_arc_data、UF_CURVE_ask_line_data)

    UF_initialize(); //CSYS_ask_wcs tag_t wcs_id=NULL_TAG; //CSYS_ask_csys_info ]={0.0}; //CURVE_create_ ...

  2. ent 基本使用 三 边(关系处理)

    ent 提供了图查询的能力,实际上在关系数据库中的表现就是relation,以下代码接前文 添加边(关系) 添加schema entc init Car Group 效果: 添加字段 car pack ...

  3. select函数及fd_set介绍

    1. select函数 1. 用途 在编程的过程中,经常会遇到许多阻塞的函数,好像read和网络编程时使用的recv, recvfrom函数都是阻塞的函数,当函数不能成功执行的时候,程序就会一直阻塞在 ...

  4. 用户生命周期(User Lifetime)

    什么是用户生命周期? 用户生命周期是从用户开始接触产品到离开产品的整个过程.用户生命周期可分为:引入期.成长期.成熟期.休眠期.流失期.对应的是用户对产品不同的参与程度. 用户生命周期有什么用? 按照 ...

  5. nginx架构分析之 事件驱动模型

    事件驱动模型 事件驱动模型是实现异步非阻塞的一个手段.事件驱动模型中,一个进程(线程)就可以了. 对于web服务器来说,客户端A的请求连接到服务端时,服务端的某个进程(Nginx worker pro ...

  6. Oracle 的查询组合语句

    select   a.core_txn_srl_no||a.c_dept||a.c_batch||lpad(a.c_opr_no,5,'0')||case a.txn_dr_cr_ind when ' ...

  7. 原生js实现元素类名的判存、添加和移除

    1.addClass:为指定的dom元素添加样式. 2.removeClass:删除指定dom元素的样式. 3.toggleClass:如果存在(不存在),就删除(添加)一个样式. 4.hasClas ...

  8. Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?

    本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...

  9. C# Microsoft.Office.Interop.Owc11 导出excel文件

    C# Microsoft.Office.Interop.Owc11 导出excel文件 1.新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86) 注意:因为大多数第三方写的 ...

  10. HIVE-计算累计和

    eg:统计1-12月的累积销量,即1月为1月份的值,2月为1.2月份值的和,3月为123月份的和,12月为1-12月份值的和 SELECT   month,SUM(amount) month_amou ...