spring - 带变量的 thymeleaf 绝对 URL

标签 spring spring-mvc thymeleaf

我正在尝试使用 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/

相关文章:

java - 使用 Thymeleaf 显示净化后的 HTML 的最干净方式?

java - 如何获得 thymeleaf 解析器的结果(html)?

java - 如何为数据库连接创建不同的类

java - 应用程序启动失败 : Spring Boot setEnableLoggingRequestDetails() not exist

hibernate - 域模型对象在层之间传递开销吗?

jsp - 在带有 Spring 的 JSP 中使用什么进行本地化?

thymeleaf - 第 :block and th:remove ="tag" adding undesired spaces

java - 加载多个 YAML 文件(使用@ConfigurationProperties?)

java - 在 WebSphere Application Server 8 上部署 Spring MVC 3 Rest

java - SimpMessageHeaderAccessor vs StompHeaderAccessor Spring + java之间的主要区别是什么