Thymeleaf/springmvc-如何在链接表达式中嵌套变量/表达式?
例如,Spring中的控制器方法执行以下操作:Thymeleaf/springmvc-如何在链接表达式中嵌套变量/表达式?,spring,spring-mvc,template-engine,thymeleaf,Spring,Spring Mvc,Template Engine,Thymeleaf,例如,Spring中的控制器方法执行以下操作: model.addAttribute("view_name", "foobar") 我正试图在我的Thymeleaf模板中做到这一点: <link th:href="@{/resources/libs/css/${view_name}.css}" rel="stylesheet" /> 但渲染结果如下所示: <link href="/app/resources/libs/css/${view_name}.css" rel=
model.addAttribute("view_name", "foobar")
我正试图在我的Thymeleaf模板中做到这一点:
<link th:href="@{/resources/libs/css/${view_name}.css}" rel="stylesheet" />
但渲染结果如下所示:
<link href="/app/resources/libs/css/${view_name}.css" rel="stylesheet" />
所以它没有像我期望的那样替换${view\u name}
我做错了什么?一般来说,您如何在Thymeleaf中嵌套这样的表达式?因为您不是用表达式(例如,
${…}
,{…}
,{…}
,{124;…}
,{quoted string'
,'quoted string'
,…),TyeleleAF将将整个表达式视为<代码>字符串< /代码>,不执行任何内部表达式。
下面的示例可以工作,因为它以表达式开头
@{${attribute}}
对于您的示例,您有以下几种可能性
文字子项(首选方法)
您可以使用管道语法(|
)在字符串中执行文字子语句
字符串串联
<link th:href="@{'/resources/libs/css/' + ${view_name} + '.css'}" rel="stylesheet" />
谢谢!为什么Literal substation是首选方法?您知道如何取消对自定义属性的扫描吗?我正在尝试执行th:attr=“ng include=@{/resources/html/report tab divs/mainreport.html}”
输出ng include=“/hp dsat/resources/html/report tab divs/supported data report.html”
,但我确实希望它输出ng include=“”/hp dsat/resources/html/report tab divs/supported data report.html'
(注意额外的单引号)。我如何让ThymeLeaf在双引号中用单引号给我完整的上下文路径?我尝试在父元素上设置th:inline=“none”
,然后执行ng include=“[[@{/resources/html/report tab divs/mainreport.html}]”
但这也不行;ThymeLeaf不去管它。非常感谢!我在任何地方都找不到文字替换,但它正是我所需要的。@trusktr你用'''.''''.''
试过吗?(三重单引号)
<link th:href="@{'/resources/libs/css/' + ${view_name} + '.css'}" rel="stylesheet" />