2016-01-21 111 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}"/>