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的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- window 下要运行php,需要编辑php环境变量
参考:https://blog.csdn.net/zhezhebie/article/details/72765262
- [学习笔记] Uplift Decision Tree With KL Divergence
Uplift Decision Tree With KL Divergence Intro Uplift model 我没找到一个合适的翻译,这方法主要应用是,探究用户在给予一定激励之后的表现,也就是 ...
- 实用的60个CSS代码片段[下]
31.有趣的& .amp { font-family: Baskerville, 'Goudy Old Style', Palatino, 'Book Antiqua', serif; fon ...
- mandatory argument 'crshome' is missing
1. 错误信息 在oracle 10.2.0.4 to 11.1.0.6 的各个版本中,尽管变量ORA_CRS_HOME设置正确,也会遇到如下错误: # ./diagcollection.pl -c ...
- 图片加载框架之ImageLoader
Android开发中,多少会接触到异步加载图片,或者加载大量图片的问题,而加载图片我们常常会遇到许多的问题,比如说图片的错乱,OOM等问题,对于这些问题解决起来会比较吃力,比较著名的就是Univers ...
- react判断点击位置是否为组件内,实现点击外部触发组件内事件
1.导入 import {findDOMNode} from 'react-dom' 2.绑定ref <div ref="refTest" </div> 3.绑定 ...
- SSM详细整合
SSM(Spring+Spring MVC+MyBatis) 1 简介 SSM 包含三大框架: Spring MVC (web level),采取 MVC 架构,意图取代麻烦的 Servlet 写法, ...
- 用Tcpdump抓包
在安卓手机上抓包 1.将手机root并连上,记得开启开发者选项,并选择传输文件 2.将tcpdump程序拷到手机里面,可以直接在电脑上操作,也可以用adb 3.使用adb操作手机内核,安卓内核基于li ...
- Nginx代理与反向代理、负载均衡实
通过 Nginx 提供的反向代理和负载均衡功能,可以合理的完成业务的分配,提高网站的处理能力:同时利用缓存功能,还可以将不需要实时更新的动态页面输出结果,转化为静态网页形成缓存,从而提高网站的响应速度 ...
- Smarty快速入门
1.Smarty是用纯php语言写的类 2.功能是实现前后端分离 3.Smarty简洁高效 4.快速入门案例 1.下载 smarty源码 https://www.smarty.net/ 2.搭建PHP ...