1、简介

thymleaf是一个基于html的页面模板,springboot极力推荐使用它,代替jsp。

API地址:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

2、使用

1)添加依赖

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

2)在templates下添加一个html,将文档声明为thymleaf文档:

<html xmlns:th="http://www.thymeleaf.org">

3)写一个控制器

package com.biniu.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author lay
* @date 2018/4/15.
* @time 12:53
*/
@Controller
public class IndexController { @RequestMapping(value = {"", "index"})
public String index(HttpServletRequest request, Model model) {
// 属性
model.addAttribute("properties", "properties value");
// 对象
Map<String, Object> person = new HashMap<>();
person.put("name", "lay");
model.addAttribute("person", person);
// 列表
List<Map<String, Object>> persons = new ArrayList<>();
persons.add(person);
persons.add(person);
persons.add(person);
model.addAttribute("persons", persons);
// request
request.setAttribute("requestAttribute", "requestValue");
// session
request.getSession().setAttribute("sessionAttribute", "sessionValue");
return "index";
}
}

4)thymleaf基本功能使用示例

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>welcome</title>
<!--引入资源-->
<link rel="stylesheet" th:href="@{/css/index.css}"/>
</head>
<body> 属性:
<div th:text="${properties}"></div>
<hr/> 对象:
<div th:text="${person.name}"></div>
<hr/> if:
<div th:if="${1 <= 2}">if true</div>
<hr/> switch-case:
<div th:switch="1">
<div th:case="1">case 1</div>
<div th:case="2">case 1</div>
<div th:case="3">case 1</div>
<div th:case="4">case 1</div>
</div>
<hr/> 遍历:
<div th:each="item, stat: ${persons}">
<span th:text="${item.name}"></span>:&nbsp;<span th:text="${stat.index}"></span>
</div>
<hr/> 跳转:
<a th:href="@{http://www.baidu.com}">百度</a>
<hr/> 图片:
<img th:src="@{https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=708140547,444241267&fm=27&gp=0.jpg}" alt="" style="display: block;width: 100px;height: 100px;">
<hr/> request取值:
<div th:text="${requestAttribute}"></div>
<hr/> session取值:
<div th:text="${session.sessionAttribute}"></div>
<hr/>
</body>
</html>

三、thymeleaf的使用的更多相关文章

  1. SpringBoot(三)thymeleaf+JPA+MySql

    接着上一节的 第一步:在pom文件中加入以下代码: <!--JPA--> <dependency> <groupId>org.springframework.boo ...

  2. Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用

    前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...

  3. spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)

    一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别 insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部 ...

  4. vert.x学习(三),Web开发之Thymeleaf模板的使用

    在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...

  5. Spring boot(三)整合mybaties+thymeleaf实现基础crud

    工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  6. 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型

    项目源码:https://github.com/y369q369/springBoot.git      ->     thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...

  7. Spring MVC : Java模板引擎 Thymeleaf (三)

    以下以构造一个表单開始,解说 Thymeleaf的使用方法. 为了演示方便,还是以经典的注冊为例. 这是Thymeleaf的form的形式, <form action="#" ...

  8. 三、SpringBoot整合Thymeleaf视图

    目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...

  9. 转--Spring MVC : Java模板引擎 Thymeleaf (三)

    原文:http://www.csdn.com/html/topnews201408/49/1349.htm 下面以构造一个表单开始,讲解 Thymeleaf的用法.为了演示方便,还是以经典的注册为例. ...

随机推荐

  1. Python进程间通信之共享内存

    前一篇博客说了怎样通过命名管道实现进程间通信,但是要在windows是使用命名管道,需要使用python调研windows api,太麻烦,于是想到是不是可以通过共享内存的方式来实现.查了一下,Pyt ...

  2. OCP 12c最新考试原题及答案(071-6)

    6.(4-21) choose the best answer: View the Exhibit and examine the structure of the CUSTOMERS table. ...

  3. js计算机样式window.getComputedStyle(ele,null)与

    一.getComputedStyle兼容IE8以上,范户籍的计算样式的值都是绝对值,没有相对单位(如:width:10em; 它打印出来是160px) window.getComputedStyle( ...

  4. AOP之 Filter实用

    前言 开心一笑~~~ 一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面是一个老奶奶和一个年轻漂亮的姑娘.两个上前坐了下来.程序 ...

  5. 【spring】Spring Boot:定制自己的starter

    概念 在学习Spring Boot的过程中,接触最多的就是starter.可以认为starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring ...

  6. [译文]casperjs使用说明-使用命令行

    使用命令行 Casperjs使用内置的phantomjs命令行解析器,在cli模块里,它传递参数位置的命名选项 但是不要担心不能熟练操控CLI模块的API,一个casper实例已经包含了cli属性,允 ...

  7. js的事件机制二

    js的事件机制二 1.给合适的HTML标签添加合适的事件 onchange-----select下拉框 onload-----body标签 单双击-----用户会进行点击动作的HTML元素 鼠标事件 ...

  8. POJ-3126-Prime Path(BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27852   Accepted: 15204 Desc ...

  9. python学习,day3:函数式编程,带return

    return的主要作用就是,在调用的时候,能知道函数的运行情况,相当于打个标签 # coding=utf-8 # Author: RyAn Bi def test1(): print('in the ...

  10. Vue项目中使用HighChart

    记:初次在Vue项目中使用 HighChart 的时候要走的坑 感谢这位哥们的思路 传送门 Vue-cli项目使用 npm install highcharts --save 让我们看看 highch ...