2016-01-21 113 views
3

我想顯示用戶頭像,如果它存在,並顯示一個默認頭像,如果不存在。我使用的代碼是:Thymeleaf conditional img src

<img src="/images/commentavatar1.png" th:src="${comment.user.image} != null ? ${comment.user.image} : '/images/default-user.png'" th:alt="${comment.user.nameSurname}"/> 

我看到的只是alt標記。呈現的元素具有空的src屬性。謝謝。

回答

3

嘗試

<img th:src="${(comment.user.image != null && !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/> 
+0

非常感謝你。這有效地檢查了字符串是否爲空,這與我們的後端完全相同。 Большоеспасибо,удачи。 –

0

嘗試..變化&&AND

<img th:src="${(comment.user.image != null && !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/> 

<img th:src="${(comment.user.image != null AND !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/>