Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymleaf switch statement with multiple case

Tags:

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

like image 314
Faraj Farook Avatar asked Apr 15 '15 18:04

Faraj Farook


People also ask

How do you use the switch case in Thymeleaf?

In Java, you are familiar with switch/case structure. Thymeleaf also has a similar structure that is th:swith/th:case. If there are more than two possible results of an expression, we can use the th:switch and th:case attributes for the conditional rendering of the HTML elements.

How do you add multiple statements to a switch case?

Use break keyword to stop the execution and exit from the switch. Also, you can write multiple statements in a case without using curly braces { }. As per the above syntax, switch statement contains an expression or literal value. An expression will return a value when evaluated.

Can you have multiple switch statements?

There can be any number of case statements within a switch. Each case is followed by the value to be compared to and after that a colon.

What is #{} in Thymeleaf?

Variable expressions are OGNL expressions –or Spring EL if you're integrating Thymeleaf with Spring. *{} is used for selection expressions. Selection expressions are just like variable expressions, except they will be executed on a previously selected object. #{} is used for message (i18n) expressions.


2 Answers

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> 
like image 151
pens-fan-69 Avatar answered Sep 21 '22 12:09

pens-fan-69


I hit the same issue today, where the object used in my th:switch is a Java enum. I eventually figured out that this was a Java equals() versus == issue. In the th:switch I have an enum object, but in my th:case I have a String object. I solved it by using a Thymeleaf string function to convert the enum object to a string, and then everything works.

  <div th:switch="${#strings.toString(datafile.status)}">       <td th:case="'SUCCESS'" class="table-success">SUCCESS</td>       <td th:case="'FAILED'" class="table-danger">FAILED</td>       <!-- default case -->       <td th:case="*" th:text="${#strings.toString(datafile.status)}" class="table-secondary">xxx</td>   </div> 

In my example above I'm using the switch to conditionally apply a Bootstrap style to a table cell.


An alternate solution is to perform the logic in Java code and expose the output value as an object property, then just reference that property in the Thymeleaf template. Something like this:

public String getBootstrapTableRowClassForStatus() {     Objects.requireNonNull(status);     switch (status) {         case SUCCESS:             return "table-success";         case FAILED:             return "table-danger";         case PROCESSING:             return "table-info";         default:             return "table-secondary";     } } 

and then I use Thymeleaf th:class:

<tr th:class="${datafile.bootstrapTableRowClassForStatus}"> 

On my page, this will apply a Bootstrap color style to my table row based on the value of my Status enum in the Java object.

like image 43
Jim Tough Avatar answered Sep 18 '22 12:09

Jim Tough