tijn167 tijn167 - 19 days ago 7
Java Question

How to only display variables in thymeleaf which ain't null

I've got a List with image url's and I only want to display the one's that are not null. But i've tried many things and nothing seems to work.

This is the HTML code:

<ul class="first">
<div th:each="image : ${car.getImageUrl()}">
<li class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<a th:href="${image}" class="pop" data-id="${image}">
<img class="img-responsive thumbnailsmaller" th:src="${image}"/>
</a>
</li>
</div>
</ul>


And this is the output:

Picture

Answer

You can try th:if like this:

<ul class="first">
 <div th:each="image : ${car.getImageUrl()}" th:if="${!#strings.isEmpty(image)}">
     <li class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
        <a th:href="${image}" class="pop" data-id="${image}">
            <img class="img-responsive thumbnailsmaller" th:src="${image}"/>
        </a>
     </li>
 </div>         
</ul>

Also, make sure your image urls are valid, test them with chrome developer tools and see if there is no url for those images, because your urls may also be broken because of a reason.