带有多个 case 的 Thymleaf switch 语句 [英] Thymleaf switch statement with multiple case

查看:55
本文介绍了带有多个 case 的 Thymleaf switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之

我想在 thymeleaf 中使用 switch 语句,一旦写入多个 case 语句就具有逻辑.

详细

我想在百里香叶中实现这个

switch(status.value){案例完成":案例无效"://打印考试未激活打破;案例新"://打印考试是新的和活动的打破;}

我当前的 thymleaf 代码因运行时错误而失败

 

<div th:case="'COMPLETE','INVALID'"><!-- 打印对象未激活-->

<div th:case="NEW'"><!-- 打印对象是新的并且处于活动状态-->

但是上面的代码失败并报错

<块引用>

org.thymeleaf.exceptions.TemplateProcessingException:无法解析为表达式:'COMPLETE','INVALID'"...

注意:我知道上述错误消息的原因.我所需要的只是知道一种为单个输出实现多情况切换的方法

解决方案

失败是因为在第一种情况下您没有有效的表达式.具体来说,

'COMPLETE','INVALID'

不是有效的表达式.如果状态为 COMPLETE 或 INVALID,我怀疑您尝试做的是包含 div.不幸的是,我相信您必须单独复制这些条件的标记.让我建议以下标记:

<th:block th:switch="${status.value}"><div th:case="'完成'"><!-- 打印对象未激活-->

<div th:case="'无效'"><!-- 打印对象未激活-->

<div th:case="'NEW'"><!-- 打印对象是新的并且处于活动状态-->

</th:block>

或者,您可以求助于 th:if 在这种情况下实际上可能会更好:

<!-- 打印对象未激活-->

<div th:if="${status.value} eq 'NEW'"><!-- 打印对象是新的并且处于活动状态-->

或者更简单:

<!-- 打印对象未激活-->

<div th:if="${status.value} eq 'NEW'"><!-- 打印对象是新的并且处于活动状态-->

In Short

I want to have switch statement in thymeleaf with logic once written to multiple case statements.

In detail

I want to implement this in the thymeleaf

switch(status.value){
  case 'COMPLETE':
  case 'INVALID':
     //print exam is not active
     break;
  case 'NEW':
     //print exam is new and active
     break;
}

My current thymleaf code which fails with runtime error

 <div th:switch="${status.value}">
      <div th:case="'COMPLETE','INVALID'">
         <!-- print object is not active -->
      </div>
      <div th:case="NEW'">
         <!-- print object is new and active -->
      </div>
 </div>                             

But the above code fails with error

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "'COMPLETE','INVALID'"...

Note: I know the reason for this above error message. All I need is to know a way to implement switch with multiple cases for a single output

解决方案

The failure is due to the fact that you don't have a valid expression in the first case. Specifically,

'COMPLETE','INVALID'

is not a valid expression. I suspect that what you are trying to do is include the div if the status is COMPLETE or INVALID. Unfortunately, I believe you will have to duplicate the markup for those conditions individually. Let me suggest the following markup:

<!-- th:block rather than unneeded div -->
<th:block th:switch="${status.value}">
    <div th:case="'COMPLETE'">
        <!-- print object is not active -->
    </div>
    <div th:case="'INVALID'">
        <!-- print object is not active -->
    </div>
    <div th:case="'NEW'">
        <!-- print object is new and active -->
    </div>
</th:block>

Alternatively you could resort to th:if which might actually work better in this case:

<div th:if="${status.value} eq 'COMPLETE' or ${status.value} eq 'INVALID'">
    <!-- print object is not active -->
</div>
<div th:if="${status.value} eq 'NEW'">
    <!-- print object is new and active -->
</div>

Or even more simply:

<div th:unless="${status.value} eq 'NEW'">
    <!-- print object is not active -->
</div>
<div th:if="${status.value} eq 'NEW'">
    <!-- print object is new and active -->
</div>

这篇关于带有多个 case 的 Thymleaf switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆