Thymeleaf 条件 img src

moo*_*ker 6 html java spring thymeleaf

如果存在,我想显示用户头像,如果不存在则显示默认头像。我使用的代码是:

<img src="/images/commentavatar1.png" th:src="${comment.user.image} != null ? ${comment.user.image} : '/images/default-user.png'" th:alt="${comment.user.nameSurname}"/>
Run Code Online (Sandbox Code Playgroud)

我看到的只是 alt 标签。渲染的元素有一个空的 src 属性。谢谢你。

Myk*_*nko 5

尝试

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