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:switchth: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转义输出

原文地址:https://www.cnblogs.com/jasonboren/p/11395448.html