【问题标题】:Thymeleaf show another image if not existsThymeleaf 如果不存在则显示另一张图片
【发布时间】:2018-04-29 06:10:35
【问题描述】:

我正在使用以下图像代码,该代码仅在图像存在时才显示。

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

我想为 else caluse 显示这个

<img height="150" width="150" src="img/NoPicAvailable.png" /> 

Thymeleaf 如何做到这一点?

【问题讨论】:

  • 为什么不只是&lt;img th:if="${ad.adPhotos.empty}" height="150" width="150" th:src="img/NoPicAvailable.png" /&gt;?甚至使用th:switch
  • @bphilipnyc 我结束了使用th:if="${ad.adPhotos.empty}"
  • 另一种方法th:src= "${ad.adPhotos.empty ? 'img/NoPicAvailable.png' : '/imageDisplay?id=' + ad.adPhotos[0].id}"
  • @MohamedSanaulla 我也喜欢你的方法

标签: thymeleaf


【解决方案1】:

我个人会这样做:

<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" />

作为旁注,我想我会使用 thymeleaf url 语法生成 src,而不是字符串连接。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多