我想显示用户头像(如果存在),如果不存在则显示默认头像。我使用的代码是:
<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 属性。谢谢。
最佳答案
尝试
<img th:src="${(comment.user.image != null && !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/>
关于java - Thymeleaf 条件 img src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34922652/