if、unless、switch
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 - 循环