SpringBoot 逻辑判断

所有的页面模板都一定存在有各种基础逻辑,例如:判断、循环处理操作。

在 Thymeleaf 之中对于逻辑可以使用如下的一些运算符来完成,例如:and、or、关系比较(>、<、>=、

<=、==、!=、lt、gt、le、ge、eq、ne)。

1、通过控制器传递一些属性内容到页面之中:

	@RequestMapping(value = "/message/member_show", method = RequestMethod.GET)
	public String memberShow(Model model) {
		Member2 vo = new Member2();
		vo.setMid(101L);
		vo.setName("阿三");
		vo.setAge(9);
		vo.setSalary(9999.99);
		vo.setBirthday(new Date());
		model.addAttribute("member",vo);
		return "message/member_show";
	}

member_show.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>SpringBoot模板渲染</title>
	<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />
	<meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body>

	<span th:if="${member.age lt 18}">
	未成年人!
	</span>
	<span th:if="${member.name eq '阿三'}">
	欢迎小三来访问!
	</span>

</body>
</html>

http://localhost/message/member_show

未成年人! 欢迎小三来访问!
2、除了这种做法之外还可以实现不满足条件的判断:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>SpringBoot模板渲染</title>
	<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />
	<meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body>

	<span th:unless="${member.age gt 18}">
	你还不满18岁,不能够看电影!
	</span>

</body>
</html>

http://localhost/message/member_show

你还不满18岁,不能够看电影!
3、在开发之中还会使用到switch这样的语句来进行多内容的判断

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>SpringBoot模板渲染</title>
	<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />
	<meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body>

	<span th:switch="${member.mid}">
		<p th:case="100">uid为101的员工来了</p>
		<p th:case="99">uid为102的员工来了</p>
		<p th:case="*">没有匹配成功的数据!</p>
	</span>

</body>
</html>

http://localhost/message/member_show

没有匹配成功的数据!

在thymeleaf之中实现的switch语句之中并没有default的存在,如果要使用default功能就采用th:case="*"

的模式完成匹配处理.

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/88118728
今日推荐