2017-11-15 1 views
0

Ich verwende den folgenden Bildcode, der ein Bild nur zeigt, wenn es existiert.Thymoleaf zeigen ein anderes Bild, wenn nicht existiert

<img th:if="${!ad.adPhotos.empty}" th:src="|/imageDisplay?id=${ad.adPhotos[0].id}|" alt="" class="img-respocive" /> 

Ich möchte diese caluse

für den anderen zeigen
<img height="150" width="150" src="img/NoPicAvailable.png" /> 

Wie dies mit Thymeleaf geschehen würde?

+1

Warum nicht einfach ''? Oder benutze 'th: switch'? – bphilipnyc

+0

https://stackoverflow.com/questions/13494078/how-to-do-if-else-in-thymeleaf – bphilipnyc

+0

@ bphilipnyc ich endete mit 'th: if =" $ {ad.adPhotos.empty} "' – Arya

Antwort

0

Ich persönlich würde es tun wie folgt aus:

<img th:unless="${ad.adPhotos.empty}" th:src="|/imageDisplay?id=${ad.adPhotos[0].id}|" alt="" class="img-respocive" /> 
<img th:if="${ad.adPhotos.empty}"height="150" width="150" src="img/NoPicAvailable.png" /> 

Als Nebenbemerkung, ich glaube, ich würde das src mit thymeleaf URL-Syntax erzeugen, anstatt die String-Verkettung.

<img th:unless="${ad.adPhotos.empty}" th:src="@{/imageDisplay(id=${ad.adPhotos[0].id})}" alt="" class="img-respocive" /> 
Verwandte Themen