kashipai
kashipai

Reputation: 157

Thymeleaf th:if expression not evaluating true

I am trying to evaluate an expression with two strings, its evaluating to true as well, but when used within the th:if its not working as expected.

the below code is what I am trying

<div th:with="cntx=${#httpServletRequest.getRequestURI()}">
   <li th:each="obj : ${list}" th:with="path=${obj.path}"
                    th:if="${path == cntx}">
    <span th:text="${obj.title}"></span>
   </li>
</div>

The context for the above code,

I have a list of objects which contain the link that has to be displayed in one of the objects. I am trying to a string equality with th:if, but the expression here ${path == cntx} is failing for some reason and not showing on the final rendered page.

I even checked the path and URI values which is equal i.e /test for path and /test for cntx, evaluating to true if I print it out using the th:text. Quite strange behaviour.

Upvotes: 1

Views: 4500

Answers (1)

NoDataFound
NoDataFound

Reputation: 11979

My guess is that the if is evaluated before the th:with. You could try :

<th:block th:each="obj : ${list}" th:with="path=${obj.path}">
  <li  th:if="${path == cntx}">...</li>
</th:block>

However, for me, it would have print some kind of useful exception (like: variable path not found).

Upvotes: 2

Related Questions