三、thymeleaf的使用
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>: <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的使用的更多相关文章
- SpringBoot(三)thymeleaf+JPA+MySql
接着上一节的 第一步:在pom文件中加入以下代码: <!--JPA--> <dependency> <groupId>org.springframework.boo ...
- Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用
前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...
- spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)
一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别 insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部 ...
- vert.x学习(三),Web开发之Thymeleaf模板的使用
在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...
- Spring boot(三)整合mybaties+thymeleaf实现基础crud
工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...
- 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型
项目源码:https://github.com/y369q369/springBoot.git -> thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...
- Spring MVC : Java模板引擎 Thymeleaf (三)
以下以构造一个表单開始,解说 Thymeleaf的使用方法. 为了演示方便,还是以经典的注冊为例. 这是Thymeleaf的form的形式, <form action="#" ...
- 三、SpringBoot整合Thymeleaf视图
目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...
- 转--Spring MVC : Java模板引擎 Thymeleaf (三)
原文:http://www.csdn.com/html/topnews201408/49/1349.htm 下面以构造一个表单开始,讲解 Thymeleaf的用法.为了演示方便,还是以经典的注册为例. ...
随机推荐
- windows server2008 r2安装DNS服务器
1.开始->管理工具->服务器管理器 2.角色->添加角色 3.服务器角色->DNS服务器 4.一直点击下一步,直至安装完成. (确认步骤时会提示,可能会需要重启服务器) 安装 ...
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
对于 short s1 = 1; s1 = s1 + 1;由于 s1+1运算时会自动提升表达式的类型,所以结果是 int型,再赋值给 short 类型 s1时, 编译器将报告需要强制转换类型的错误.对 ...
- 虚拟机上使用 opecnv 读取USB摄像头无法显示
使用opecv读取USB摄像头时候,无法显示图像. 设置 首先查看虚拟机Ubuntu检测摄像头是否已正常插入: ls /dev/video* 结果为: 设置虚拟机USB属性: USB的兼容性设置为US ...
- Kettle根据表输入的SQL脚本返回创建表的SQL脚本
其中[物料分组]节点就是表输入,里面可以构建SQL查询.那么怎么把生成的数据插入到目标表中?并且构建目标表的结构呢? 目标表结构就在[表输出]栏位 点击[SQL],就可以看见生成目标表结构的脚本
- 包子凑数(dp思想)
问题描述: 小明几乎每天早晨都会在一家包子铺吃早餐.他发现这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子.每种蒸笼都有非常多笼,可以认为是无限笼.每当有顾客想买X个包子,卖包子的大叔就会迅速选 ...
- sql查询字段中的值长度最大的记录
SELECT max(length(字段)) FROM 表名;
- python 面向过程和面向对象比较
面向过程 VS 面向对象 面向过程的程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是:复杂度 ...
- Vue学习笔记 template methods,filters,ChromeDriver,安装sass
ChromeDriver installation failed Error with http(s) request: Error: connect ETIMEDOUT 172.217.160.80 ...
- CentOS系统服务配置资源限制ulimit
使用Systemd管理服务的,均可使用此方法. 在 /usr/lib/systemd/system/xxx.service中,添加如下内容即可: [Service] # Other directive ...
- Android Notification 的四种使用方式
实现通知步骤 一般实现通知需要如下步骤: 1.获取 NotificationManager 实例管理通知: 2.实例 Notification 对象: 3.管理事件 Intent: 4.发送通知. 注 ...