springboot页面模板thymeleaf的简单用法
thymeleaf基础语法:
变量输出与字符串操作:
th:text 表示在页面输出值
th:value 表示将一个值放入input标签的value中
判断字符串是否为空:
thymeleaf内置对象:调用内置对象需要#开头。并且大部分的内置对象都是以s结尾,例如dates
${#strings.isEmpty(key)} :判断字符串是否为空,为空返回true,否则为false
${#strings.contains(msg,'T')} :判断字符串是否包含指定的子串,包含为true,否则为false
${#strings.startWith(msg,'a')} :判断字符串是否为某个子串开头。是为true,不是为false
${#strings.endsWith(msg,'a')}:判断当前字符串是否以子串结尾,如果是返回true,否则返回false
${#strings.length(msg)}:返回字符串的长度
${#strings.indexOf(msg,'h')}:查找子串的位置,并返回该子串的下标,如果没找到则返回-1
${#strings.substring(msg,13)}
${#strings.substring(msg,13,15)}:都表示截取字符串
${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}:表示转大写和小写
日期和数字格式化处理
日期和数字格式化处理:#numbers,#dates
${#dates.format(key)}:格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key,'yyy/MM/dd')}:按照自定义的格式做日期转换
${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}:以上分别表示为:取年,月,日
Thymeleaf条件判断
th:if(条件成立时显示)
和th:unless(条件不成立时显示)
<span th:if="${sex} == '男'">
性别:男
</span>
<span th:if="${sex} == '女'">
性别:女
</span>
th:switch
和th:case
为一组表示多分支选择语句
<div th:switch="${id}">
<span th:case="1">ID为1</span>
<span th:case="2">ID为2</span>
<span th:case="3">ID为3</span>
</div>
迭代遍历
th:each
@RequestMapping("/show3")
public String showInfo3(Model model){
List<Users> list = new ArrayList<>();
list.add(new Users(1,"张三",20));
list.add(new Users(2,"李四",22));
list.add(new Users(3,"王五",24));
model.addAttribute("list", list);
return "index3";
}
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="u : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
</tr>
</table>
状态变量
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Index</th>
<th>Count</th>
<th>Size</th>
<th>Even</th>
<th>Odd</th>
<th>First</th>
<th>lase</th>
</tr>
<tr th:each="u,var : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
<td th:text="${var.index}"></td>//当前迭代器的索引从0开始
<td th:text="${var.count}"></td>//当前迭代对象的计数 从1开始,可以增加一列为序号功能
<td th:text="${var.size}"></td>//被迭代对象的长度
<td th:text="${var.even}"></td>//布尔值,当前循环是否是偶数/奇数 从0开始
<td th:text="${var.odd}"></td>
<td th:text="${var.first}"></td>//当前循环的是否是第一条如果是返回true否则返回false
<td th:text="${var.last}"></td>
</tr>
</table>
状态变量属性
1,index:当前迭代器的索引 从0开始
2,count:当前迭代对象的计数 从1开始
3,size:被迭代对象的长度
4,even/odd:布尔值,当前循环是否是偶数/奇数 从0开始
5,first:布尔值,当前循环的是否是第一条,如果是返回true否则返回false
6,last:布尔值,当前循环的是否是最后一条,如果是则返回true否则返回false
th:each迭代Map集合
@RequestMapping("/show4")
public String showInfo4(Model model){
Map<String, Users> map = new HashMap<>();
map.put("u1", new Users(1,"张三",20));
map.put("u2", new Users(2,"李四",22));
map.put("u3", new Users(3,"王五",24));
model.addAttribute("map", map);
return "index4";
}
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="maps : ${map}">
<td th:each="entry:${maps}" th:text="${entry.value.userid}" ></td>
<td th:each="entry:${maps}" th:text="${entry.value.username}"></td>
<td th:each="entry:${maps}" th:text="${entry.value.userage}"></td>
</tr>
</table>
域对象操作
1.HttpServletRequest
request.setAttribute("req", "HttpServletRequest");
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>
2.HttpSession
request.getSession().setAttribute("sess", "HttpSession");
Session:<span th:text="${session.sess}"></span><br/>
3.ServletContext servlet上下文
request.getSession().getServletContext().setAttribute("app", "Application");
Application:<span th:text="${application.app}"></span>
URL表达式
th:href
th:src
语法:@{}
URL类型
绝对路径:<a th:href="@{http://www.baidu.com}">绝对路径</a>
相对路径:
相对于项目的上下文的相对路径:<a th:href="@{/show}">相对路径</a>
相对于服务器路径的根:<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
在url中实现传递参数:<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
在url中通过restful风格进行参数传递
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参-restful</a>
一.删除模板片段使用th:remove属性
th:remove的值如下:
1.all:删除包含标签和所有的孩子。
2.body:不包含标记删除,但删除其所有的孩子。
3.tag:包含标记的删除,但不删除它的孩子。
4.all-but-first:删除所有包含标签的孩子,除了第一个。
5.none:什么也不做。这个值是有用的动态评估。
参见:https://www.cnblogs.com/suncj/p/4030975.html
Thymeleaf简单格式化输出
为integer和Date属性添加格式输出:#numbers,#dates的使用
字符串连接使用+号进行连接
原样输出和转义输出 utext原样输出,text转义输出
springboot页面模板thymeleaf的简单用法的更多相关文章
- SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...
- SpringBoot入门 一 构建简单工程
环境准备:jdk1.7(推荐)以上,tomcat8(推荐)以上,或者使用插件自带.mevan插件3.2以上,eclipse编辑工具 pom文件基本配置如下 <project xmlns=&quo ...
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 从.Net到Java学习第九篇——SpringBoot下Thymeleaf
从.Net到Java学习系列目录 Thymeleaf概述 Thymeleaf 是一个流行的模板引擎,该模板引擎采用java语言开发.模板引擎是一个技术名称,是跨领域平台的概念,在java语言体系下有模 ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- ④SpringBoot之thymeleaf使用
本文介绍SpringBoot使用的模板技术thymeleaf以及通过webJar进行前端资源的引入以及使用thymeleaf介绍简单说, Thymeleaf 是一个跟 Velocity.FreeMar ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- LC 655. Print Binary Tree
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- Vue avoid mutating a prop directly since the value will be overwritten
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 内存数据库:memcached与redis技术的对比试验
本文以高性能nginx服务器为应用背景,想利用缓存技术来减轻系统负荷,加快响应时间,从而增加web服务器的吞吐量. redis是一种分布式内存数据库,memcached是一种内存缓存技术,它们都采用k ...
- debain安装文泉驿字体
sudo apt-get install ttf-wqy-microhei sudo apt-get install ttf-wqy-zenhei
- 实验一 part2
#include <stdio.h> int main () { int x; printf("输入一个整数:\n"); scanf("%d",&a ...
- 直方图匹配原理与python、matlab实现
直方图匹配本质上是让两幅图像的累积直方图尽量相似,累积直方图相似了,直方图也就相似了. 把原图像img的直方图匹配到参考图像ref的直方图,包括以下几个步骤: 1. 求出原图像img的累积直方图img ...
- python高级篇
1.切片功能:类似于java中的split方法.对list或者triple中几个值进行取出的过程. L = ['a','b','c','d'] L[0:3] = ['a','b','c'] # ...
- 【Python开发】Python之re模块 —— 正则表达式操作
Python之re模块 -- 正则表达式操作 这个模块提供了与 Perl 相似l的正则表达式匹配操作.Unicode字符串也同样适用. 正则表达式使用反斜杠" \ "来代表特殊形式 ...
- Emgu 学习笔记(8)之膨胀腐蚀
static void Main(String[] args) { Mat img = CvInvoke.Imread(); Mat d = new Mat(); Mat e = new Mat(); ...
- Python全栈开发之2、数据类型-数值、字符串、列表、字典、元组和文件处理
一.Python 运算符 1.算术运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 二.基本数据类型 1.数字整型 int(整型) 在32位机器上,整数的位数为32位,取值范围为 ...