欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

SpringBoot中使用thymeleaf的switch来实现if-else if -else的效果

发布时间:2025/3/19 javascript 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SpringBoot中使用thymeleaf的switch来实现if-else if -else的效果 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

场景

SpringBoot中使用thymeleaf的开关语句实现if-else if -else的效果。

Switch使用示例:

<div th:switch="${user.role}"><p th:case="'admin'">User is an administrator</p><p th:case="#{roles.manager}">User is a manager</p> </div>


请注意,只要一个:case属性计算为true,其它每一个th:case属性在相同的switch中认定为false。

默认选项指定为th:case="*":

<div th:switch="${user.role}"><p th:case="'admin'">User is an administrator</p><p th:case="#{roles.manager}">User is a manager</p><p th:case="*">User is some other thing</p> </div>

实现

<div th:switch="${info.status}"><th th:case="00">完成</th><th th:case="01">异常</th><th th:case="*">未知状态</th> </div>

 

总结

以上是生活随笔为你收集整理的SpringBoot中使用thymeleaf的switch来实现if-else if -else的效果的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。