首页 > 解决方案 > boolean condition check in Thymleaf is not working

问题描述

I am using Spring boot and Thymleaf in my application.I am sending a CommonResponse model from backend to view and in that model there is a property "isLoggedIn" which is a boolean value and i am using that value to add a style like this

   <li class="nav-item hide-on-scroll" id="sign_in_menu" th:style="${commonResponse.isLoggedIn == false ? '' : 'display:none'} ">
                <a class="nav-link" data-toggle="modal" data-target="#loginModal">Sign in</a>
   </li>

when i run this gives me a error

"Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "commonResponse.isLoggedIn == false ? '' : 'display:none'"

It happens when "isLoogedIn" property is part of this model "CommonResponse".but if i pass this as a standalone property it works fine

 <li class="nav-item hide-on-scroll" id="sign_in_menu" th:style="${isLoggedIn == false ? '' : 'display:none'} ">
                <a class="nav-link" data-toggle="modal" data-target="#loginModal">Sign in</a>
 </li>

What is the issue?

标签: thymeleaf

解决方案


请尝试以下操作

 <li class="nav-item hide-on-scroll" id="sign_in_menu" th:style="${commonResponse.isLoggedIn} ? '' : 'display:none' ">
                <a class="nav-link" data-toggle="modal" data-target="#loginModal">Sign in</a>
   </li>

推荐阅读