如何在 Spring 中使用 thymeleaf 将变量放入 img src 路径? [英] How to put variable to img src path in Spring with thymeleaf?
问题描述
好吧,我希望我的图像在 Front(html) 中显示表格数据.我使用 model.addAttributes 来传递玩家信息.我原来的 imgfile 在路径中:/img/
Well, I want my image show up with table data at Front(html). I use model.addAttributes to pass players information. And my original imgfile is in path : /img/
这是我的问题代码如下:
This is my problem code below :
<tbody>
<tr th:each="players : ${players}">
<td><img src="/img/ + ${players.id} + '_' + ${players.name} + '.png'" width="120"/></td>
我的图像命名总是使用id+name".png 来定义.我不知道如何将它组合到 img src 路径.我想要的解决方案是像下面这样工作:
My image naming is always define with "id+name".png. I don't know how to combine it to img src path. And What I want for solution is to working like below :
<td><img src="/img/1_pogba.png" width="120"/></td>
推荐答案
你应该使用带有 img
标签的 th:src
而不是普通的 src
> 属性.只需从此更改您的 img 标签:
You should use th:src
with img
tag instead of normal src
attribute.
Just change your img tag from this:
<img src="/img/ + ${players.id} + '_' + ${players.name} + '.png'" width="120"/>
为此:
<img th:src="@{${'/img/' + players.id +'_'+ players.name + '.png'}}" width="120"/>
有关更多信息,您可以查看此链接:
For more information you can check this link:
https://www.thymeleaf.org/doc/articles/standardurlsyntax.html
这篇关于如何在 Spring 中使用 thymeleaf 将变量放入 img src 路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!