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的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- golang 要去学习的文档记录
xrom开发文档地址: http://gobook.io/read/github.com/go-xorm/manual-zh-CN/chapter-10/ golang基础知识: https://ww ...
- Go 指针声明后赋值,出现 panic: runtime error: invalid memory address or nil pointer dereference
指针基础知识package main import "fmt" func main() { var p *int p = new(int) *p = 1 fmt.Println(p ...
- AXIS 1.4 自定义序列化/反序列化类
axis1.4的CalendarDeserializer 使用的时区是GMT,导致日期显示不准确 private static SimpleDateFormat zulu = new SimpleDa ...
- tensorflow文件读取
1.知识点 """ 注意:在tensorflow当中,运行操作具有依赖性 1.CPU操作计算与IO计算区别: CPU操作: 1.tensorflow是一个正真的多线程,并 ...
- ssh 的一个坑
概述 今天我碰到 fabric 和 ssh 的一个坑,记录下来,供以后开发时参考,相信对其他人也有用. ssh 今天用 ssh 登录远程服务器用不了 npm,查了下,发现原因是: ssh登录时不会加载 ...
- 学习使用Jmeter做压力测试(一)--压力测试基本概念
学习使用Jmeter做压力测试(一)--压力测试基本概念 一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测 ...
- 如何解决使用 JMeter 时遇到的问题
这是对 JMeter 官方网站上一篇文章的翻译.点击这里可以访问原文JMeterTroubleShooting. • check the log file. This is normally in t ...
- 如何优雅的给TDatetimePicker控件赋值(Delphi)
给DatetimePicker赋值时,可以通过界面设置赋值,也可以通过代码赋值. 通常,我们会给表示起始时间的dtp赋值为 00:00:00,给表示结束时间的dtp赋值为23:59:59. 代码如下: ...
- Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例
Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例(github代码:https://github.com/qq719862911/MemcacheTestDemo ...
- Debian10服务器安装
对于使用惯windows系统的人来说,刚开始接触使用linux系统一定是很不习惯,因为使用环境的变化经常会出现一些错误.当然,对于我来说,我也是刚刚才开始接触Linux,对此,有些地方想不到的,可以多 ...