三、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的用法.为了演示方便,还是以经典的注册为例. ...
随机推荐
- [ActionScript 3.0] 创建倒影
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Display ...
- python的数据结构分类,以及数字的处理函数,类型判断
python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...
- Postman使用手册1——导入导出和发送请求查看响应
导读: 现在的web和移动开发,常常会调用服务器提供restful接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用.这里介绍一下如何在chrome浏览器利用postma ...
- css块元素及内联元素
块级元素主要有: address , blockquote , center , dir , div , dl , fieldset , form , h1 , h2 , h3 , h4 , h5 , ...
- 快速上手日期插件daterangepicker
参数介绍: 参数 格式 示例 含义 startDate MM/DD/YYYY “startDate”:”10/14/2017” 设置默认的开始日期 endDate MM/DD/YYYY “endDat ...
- [原创]SpringBoot上传图片踩的坑
最近项目里面有个需求,要上传图片到阿里云的OSS服务.所以需要写个上传图片的接口给前端. 这个简单的接口本来就给分配了1个工时,感觉也蛮简单的.但编码过程中遇到了好几个问题,现在一一记录下来,避免再次 ...
- 洛谷 P3975 / loj 2102 [TJOI2015] 弦论 题解【后缀自动机】【拓扑排序】
后缀自动机入门. 题目描述 为了提高智商,ZJY 开始学习弦论. 这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为 \(n\) 的字符串,求出它的第 \ ...
- sql拼接列字符串
1.使用函数(sql2000以上) )) ) AS BEGIN ) select @v = isnull(@v + ',','')+code FROM dict WHERE type=@filter ...
- Pycharm+QTDesigner+PyQt5环境配置
python+PyQt5写界面很方便,记录下个人配置环境过程.... 安装软件: pycharm2017 Qt5.9.6 python3.6.6/python2.7.15 配置PyQt5: pytho ...
- Quartz .net 一直运行失败
使用了新的: Quartz.NET 2.6.2 https://github.com/quartznet/quartznet/releases IScheduler scheduler = StdSc ...