Thymleaf switch语句具有多种情况

我想在百里香中有带逻辑的switch语句,一旦写入多个case语句。

我想在百里香叶中实现

switch(status.value){

case 'COMPLETE':

case 'INVALID':

//print exam is not active

break;

case 'NEW':

//print exam is new and active

break;

}

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

 <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>

但是上面的代码失败并出现错误

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as

expression: “‘COMPLETE’,’INVALID’“…

回答:

失败的原因是在第一种情况下您没有有效的表达式。特别,

'COMPLETE','INVALID'

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

<!-- 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>

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

<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>

或更简单地说:

<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>

以上是 Thymleaf switch语句具有多种情况 的全部内容, 来源链接: utcz.com/qa/410038.html

回到顶部