
上QQ阅读APP看书,第一时间看更新
3.8 页面逻辑处理
Thymeleaf页面模板支持逻辑处理功能,如判断、循环处理等操作。开发者在页面中处理逻辑时,可以使用and、or、关系比较(>、<、>=、<=、==、!=、lt、gt、le、ge、eq、ne)等运算符来完成。
1.【mldnboot-thymeleaf项目】修改src/main/view/templates/message/message_member.html页面,追加逻辑判断。

本程序在页面中追加了判断逻辑(年龄是否为大于或等于18岁、姓名是否为指定的字符串),这样会根据传递过来的Member对象的属性进行判断,页面运行效果如图3-11所示。

图3-11 页面逻辑判断
2.【mldnboot-thymeleaf项目】在Thymeleaf之中,如果使用th:if判断条件不满足时,也可以使用th:unless处理。

3.【mldnboot-thymeleaf项目】页面中可以使用switch-case来实现开关逻辑处理。

本程序使用th:switch="*{mid}"语句对mid属性的内容进行switch判断。如果有匹配的信息,则进行内容输出;如果没有,则执行th:case="*"的信息输出,页面运行效果如图3-12所示。

图3-12 switch判断