【问题标题】:thymeleaf iterate over String to create img tagsthymeleaf 遍历 String 以创建 img 标签
【发布时间】:2016-08-04 08:37:57
【问题描述】:

我正在尝试遍历 Thymeleaf 中的字符串列表。我想将${nombre} 变量替换为它的值加上“.png”,但由于某种原因,只有 atts 标题和 alt 正在显示该值。你能帮帮我吗?

<div class="modal-body" id="modal-champions-body">
    <div th:each="nombre : ${listaNamesChamps}" style="float:left;">
        <img src="../../img/champion_icons/${nombre}.png"
             th:attr="src=@{/img/champion_icons/${nombre}.png},title=${nombre},alt=${nombre}"/>
    </div>
</div>

结果 HTML

【问题讨论】:

    标签: java html spring spring-boot thymeleaf


    【解决方案1】:

    你可以这样做:

        <img th:attr="src=@{|/img/champion_icons/${nombre}.png|},title=${nombre},alt=${nombre}" />
    

    <img th:attr="src=@{'/img/champion_icons/' + ${nombre} + '.png'},title=${nombre},alt=${nombre}" />
    

    【讨论】:

      【解决方案2】:

      如果您将使用带有th: 前缀的每个属性,它也应该是正确的:

      <img th:src="@{'/img/champion_icons/' + ${nombre} + '.png'}" 
           th:title="${nombre}" 
           th:alt="${nombre}" />
      

      链接到official tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 1970-01-01
        • 2017-03-15
        • 2011-12-03
        • 1970-01-01
        • 2011-02-26
        • 2021-12-09
        相关资源
        最近更新 更多