三、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的用法.为了演示方便,还是以经典的注册为例. ...
随机推荐
- 爬虫开发8.scrapy框架之持久化操作
今日概要 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...
- scrapy连接MongoDB
Scrapy中连接MongoDB所需要做的工作如下: 1.settings中需要设置的部分: # 启动管道组件 ITEM_PIPELINES = { 'QianChengWuYu.mongoDBPip ...
- loj #6515. 「雅礼集训 2018 Day10」贪玩蓝月
\(\color{#0066ff}{输入样例}\) 0 11 10 QU 0 0 QU 1 9 IG 14 7 IF 3 5 QU 0 9 IG 1 8 DF QU 0 4 IF 1 2 DG QU ...
- Navicat 连接腾讯云
1.dos窗口下进入mysql,进行远程登录授权 (1)进行授权 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1 ...
- 剑指offer——面试题17:打印从1到最大的n位数
用字符串模拟加法: #include"iostream" #include"string.h" using namespace std; bool AddOne ...
- python安装方法
1.下载Python安装包 2.配置环境变量 path=%path%;C:\Python27 3.安装pip 默认已安装 4.配置pip环境变量 path=%path%;C:\Python27\Scr ...
- 手机端页面调试工具-vconsole使用
用的是VUE-CLI3第一步.安装vconsole npm install vconsole 第二步.创建js文件并写入内容 import Vconsole from 'vconsole' let v ...
- UGUI优化总结
1.动静分离 canvas下元素变化时,会使整个canvas重新绘制.因此将ui经常改变和不怎么改变的部分分离,分别使用不同的canvas. 2.图集优化 不同界面的ui,可以打包成不同的图集,一些公 ...
- [JAVA]流控及超流控后的延迟处理
流控检查(每半秒累计,因此最小留空阀值只能做到每秒2条): import java.text.SimpleDateFormat; import java.util.Date; import java. ...
- Python时间time模块介绍
一.明确时间元组 二.测试代码#!/usr/bin/env python # -- coding: utf-8 --' """ 时间模块,time的相关操作与测试 &qu ...