3

I would like to display a text (or not) based on its content. For example, if status equals ACCEPTED I would like to display this word, otherwise don't. What I have is:

<td th:if="${trip.tripStatus} == 'ACCEPTED'" th:text="${trip.tripStatus}"></td>

But it looks like it doesn't work that way.

ben3000
  • 4,629
  • 6
  • 24
  • 43
jarosik
  • 4,136
  • 10
  • 36
  • 53

2 Answers2

7

@ak38 the solution you have posted is working fine but it is not perfectly correct. If you would like to compare enums you should use this notation:

<td th:if="${trip.tripStatus == T(my.package.Status).ACCEPTED}" th:text="${trip.tripStatus}"></td>

If for some reason another programmer rename this enum from ACCEPTED to ACCEPT then your code will be working unnoticeable and never met the condition while the version above will throw an exception informing that the view is not prepared for that change.

Marek Raki
  • 3,056
  • 3
  • 27
  • 50
1

Hope you got solution to this. But anyway i am posting solution, it might help others. If you are comparing ENUM type to String, convert ENUM type to String using toString method.

<td th:if="${#strings.toString(trip.tripStatus)} == 'ACCEPTED'" th:text="${trip.tripStatus}"></td>
veljkost
  • 1,748
  • 21
  • 25
akreddy.21
  • 626
  • 3
  • 8
  • 21