我正在尝试使用 Thymeleaf 将域 url 注入(inject)到链接中。我的 url 是从 Controller 传递的,因为我输入了日志并看到了它。
我的链接在我的 Thymeleaf html 模板中是这样的:
<link type="text/css" th:href="@{${DomainUrl}/web/assets/css/foundation5/foundation.min.css}" rel="stylesheet" />
但是,当我在本地运行它时,它不会替换域,例如,会引发错误(因为当然找不到 URL)呈现如下:
http://localhost:8081/pss/ui/ $%7BDomainUrl%7D/web/assets/css/components.css
最佳答案
好的,为了让它工作,你必须使用预处理运算符 __expression__
获取属性链接,因此您最终会得到这样的想法
<link type="text/css" th:href="@{__${DomainUrl}__/web/assets/css/foundation5/foundation.min.css}" rel="stylesheet" />
这将预处理和解析
${DomainUrl}
表达式,并将结果字符串传递给 @
表达式处理器。经过测试并像魅力一样工作:<a th:href="@{__${currentUrl}__/blah/blah/blahhhh}">hey there</a>
生成
<a href="http://localhost:8080/admin/place/list/blah/blah/blahhhh">hey there</a>
在哪里
http://localhost:8080/admin/place/list/
是 currentUrl
在我的
关于spring - 带变量的 thymeleaf 绝对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981145/