【发布时间】:2018-05-22 08:16:37
【问题描述】:
我有一个类似的 API:
<IP>:<Port>/getProfilePic/{userName}
此 API 仅将 CDN URL 作为字符串返回,可以在其中找到该用户的图像。在 Thymeleaf 模板中,我正在做:
<img class="chat-message-author-pic"
th:src="@{'/getProfilePic/' + ${message.getUsername()}}"
width="15px" height="15px"/>
当然,响应被视为图像内容,而不是应加载图像的 URL。就像,对于其中一位用户,浏览器中的标签显示为:
<img class="chat-message-author-pic" width="15px" height="15px"
src="/getProfilePic/shubham">
如何使用 Thymeleaf 模板调用 API,获取应作为 src 标签 URL 的字符串?这是否也适用于 Javascript 函数,当我这样做时,它将 HTML 附加到 div:
<img class="chat-message-author-pic"
th:src="@{/getProfilePic/' + username + '}"
width="15px" height="15px"/>' + ...
【问题讨论】:
-
如果您使用 JavaScript 创建 DOM 元素并附加到 DIV,那么您必须使用常规
img src属性,只要图像可访问,它就应该工作。
标签: spring-boot thymeleaf