首页 > 解决方案 > 如何在具有 src 的标签中附加 thymeleaf 变量?

问题描述

似乎无法为此找到正确的语法?

<img id='barcode' src="https://api.qrserver.com/v1/create-qr-code/?data="'+ ${var.membervar} + "'+&amp;size=100x100" alt="" title="HELLO" width="50" height="50" />

标签: javascripthtmlthymeleaf

解决方案


你应该让 Thymeleaf 知道你想src使用th:src. 像这样的东西会起作用......

<img id='barcode' th:src="'https://api.qrserver.com/v1/create-qr-code/?data='+ ${var.membervar} + '&amp;size=100x100'" alt="" title="HELLO" width="50" height="50" />

但是对于完全限定的 URL,您应该使用Thymeleaf 链接 URL,其中将为您的所有查询参数应用适当的编码。例如 ...

<img id='barcode' th:src="@{https://api.qrserver.com/v1/create-qr-code/(data=${var.membervar},size='100x100')}" alt="" title="HELLO" width="50" height="50" />

推荐阅读