【问题标题】:How to put variable to img src path in Spring with thymeleaf?如何使用百里香将变量放入 Spring 中的 img src 路径?
【发布时间】:2020-09-01 07:49:56
【问题描述】:

嗯,我希望我的图像在 Front(html) 中显示表格数据。 我使用 model.addAttributes 来传递玩家信息。 我原来的 imgfile 在路径中:/img/

这是我的问题代码如下:

<tbody>
      <tr th:each="players : ${players}">
        <td><img src="/img/ + ${players.id} + '_' + ${players.name} + '.png'" width="120"/></td>

我的图像命名总是用“id+name”.png 定义的。 我不知道如何将它与 img src 路径结合起来。 我想要的解决方案是像下面这样工作:

        <td><img src="/img/1_pogba.png" width="120"/></td>

【问题讨论】:

  • 你试过 src=`/img/${players.id}_${players.name}.png`(带反引号)吗?

标签: spring spring-boot spring-mvc thymeleaf


【解决方案1】:

您应该使用带有img 标签的th:src 而不是普通的src 属性。 只需从此更改您的 img 标签:

<img src="/img/ + ${players.id} + '_' + ${players.name} + '.png'" width="120"/>

到这里:

<img th:src="@{${'/img/' + players.id +'_'+ players.name + '.png'}}" width="120"/>

有关更多信息,您可以查看此链接:

https://www.thymeleaf.org/doc/articles/standardurlsyntax.html

【讨论】:

  • 我会推荐这样的表达式:th:src="@{/img/{id}_{name}.png(id=${players.id},name=${players.name})}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 2018-06-20
  • 2020-12-19
  • 2018-09-12
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多