Thymeleaf 无法渲染 <img>从数据库表发送时的标记 [英] Thymeleaf was unable to render <img> tag when sent from database table

查看:14
本文介绍了Thymeleaf 无法渲染 <img>从数据库表发送时的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么百里香叶不渲染

<img th:src="@{/path/img/logo.jpg}" width="50" height="50" alt="logo"/> 

当它从数据库表中读取并作为未转义文本(th:utext)放置在页面上时?

when it is read from a database table and placed on a page as an unescaped text (th:utext)?

通常放置在页面上时,它会正确呈现.这个想法是将上传的图像保存到应用程序中的路径,并将图像的名称保存为数据库表中的字符串.在读取图像期间,名称从数据库表中获取并放置在 标签中.这适用于 JSP,但我无法用 thymeleaf 获得类似的结果.我检查了我的浏览器控制台日志,路径 th:src="@{/path/img/logo.jpg}" 是可见的,但没有被解析为在页面上显示图像.

Ordinarily when placed on the page, it renders properly. The idea is to save an uploaded image to a path in the application and save the name of the image as a string in the database table. During reading of the image, the name is gotten from the database table and placed in the <img> tag. This works well with JSP's but I am unable to get similar result with thymeleaf. I checked my brower console log and the path th:src="@{/path/img/logo.jpg}" is visible but not parsed as to display the image on the page.

可能是什么问题?

推荐答案

我终于找到了解决方案.我将上下文路径附加到 src 属性的相对路径,而没有 thymeleaf 的命名空间限定,如下所示:<img src="/contextPath/path/img/logo.jpg" width="50" height="50" alt="logo"/>.注意资源的可访问性是多么简单:/contextPath/relativePath,所以最重要的路径是相对路径.这类似于:Thymeleaf 解析文本并在-文字表达.我观察到,一旦 thymeleaf 的命名空间 th: 限定了位于文本/字符串中的 hrefsrc 属性,则绝对路径未正确解析.

I finally found a solution. I attached the context path to the relative path of the src attribute without thymeleaf's namespace qualification like so: <img src="/contextPath/path/img/logo.jpg" width="50" height="50" alt="logo"/>. Notice how simple the accessibility to the resource is: /contextPath/relativePath, so the most important path there is the relative path. This is similar to: Thymeleaf parse text and execute in-text expressions. I observed that once thymeleaf's namespace th: qualifies a href or src attribute that resides inside a text/String, the absolute path is not properly resolved.

这篇关于Thymeleaf 无法渲染 &lt;img&gt;从数据库表发送时的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆