springboot-thymeleaf的使用 - 判断

th:if

返回值决定标签的显示与否,语法 th:if="${boolean表达式}"
若boolean表达式不成立、无效。则会显示标签

controller: hello方法

@RequestMapping("/hello")
 public String hello(Model model){
     model.addAttribute("name", "张三");
     model.addAttribute("age", "18");
     return "hello";
 }

hello.html:

<div th:if="${name eq '张三' and 18 != 16}">div1</div>	//true
<div th:if="${name != '张三'}">div2</div>				//false
<div th:if="${!(18 eq 16)}">div3</div>					//!false取反true

访问 http://localhost:8080/hello
在这里插入图片描述

boolean运算符
	and、or、!
比较运算符
	 >、 <、>=、<=、==、!=
	gt、lt、ge、le、eq、ne

th:unless

如果不,条件如果不成立返回true。同if一样决定标签的显示与否
语法 th:unless="${boolean表达式}"

和上面同样的代码,最后返回的结果是相反的
在这里插入图片描述

th:switch 和 th:case

后台控制器传参数

@RequestMapping("/hello")
public String hello(Model model){
    model.addAttribute("name", "张三");
    return "hello";
}

hello.html 判断 选中

<div th:switch="${name}">
    <div th:case="张三">name值为张三</div>
    <div th:case="李四">name值为李四</div>
    <div th:case="*">name值非张三和李四</div>  <!-- 其他选项为 *  -->
</div>

访问 http://localhost:8080/hello。只有符合对应值的标签才能显示
在这里插入图片描述


上一章 thymeleaf - url

下一章 thymeleaf - 循环

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值