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. oppo手机永久打开USB调试模式

    现象:十分钟不使用就会自动关闭 usb 调试模式,重新打开还得输入验证码,真尼玛烦人. 方法: 数字拨号盘 输入 *#8011#  就可以永久打开.

  2. 6.BOM

    BOM的介绍 浏览器对象模型. 操作浏览器部分功能的API.比如让浏览器自动滚动. BOM的结构图 window对象是BOM的顶层(核心)对象,所有对象都是通过它延伸出来的,也可以称为window的子 ...

  3. jquery中animate({left:'-='+width})中的 '-='+是什么意思?

    left:'-='+width的意思是:left属性的最终值,是left现有值减去width这个值 例如:left:'200px' 意思是left最终值变成200left:'+200px' 意思与上面 ...

  4. ArrayList用法详解

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  5. Python strip()函数用法

    Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格 具体示 ...

  6. 关于<ul> 下的 <li> 里面的<a> 标签字体颜色不能控制

    1.元展示 <ul class="ul"> <li><a href="#">菜单一</a></li> ...

  7. 安装php,nginx 带debug

    gdb安装包   在CentOS6.4下使用gdb进行调试的时候, 使用bt(breaktrace)命令时,会弹出如下的提示: 头一天提示: Missing separate debuginfos, ...

  8. Openfire服务端源代码开发配置指南[转]

    转自:http://www.micmiu.com/opensource/openfire/openfire-src-config/   本文将图文介绍如何把openfire(以3.8.1为例)源码配置 ...

  9. Ubuntu定时任务设置

    设置很简单,但如果误入歧途,会耽误很多时间 步骤如下: 1. 以root执行:vi /etc/crontab 2. 在文件最后添加cron配置(每天凌晨四点执行,并将日志输出到/data/cron.l ...

  10. Apache 配置代理服务

    1.根据项目需要,Apache服务下面有2个tomcat 分别指向不同的域名 2.修改 Apache-conf-httpd.conf LoadModule proxy_module modules/m ...