我试图将某些文本附加到动态文本上,如下所示:
<label th:text="Hello ${worldText}"></label>
但是UI投掷:
TemplateProcessingException: Could not parse as expression: "Hello ${worldText}
有人知道我该如何实现吗?
一个简单的解决方案是将跨度插入标签:
<label>Hello <span th:text="${worldText}"></span></label>
,但我宁愿组合这样的文本和变量:
<label th:text="'Hello' + ${worldText}"></label>
另一个直接解决方案是
<label th:text="${'Hello ' + worldText}"></label>
其他一些方法,
// 1. Using the <th:block> element
<label>Hello <th:block th:text="${worldText}"></th:block></label>
// 2. Using string concatenation
<label th:text="${'Hello ' + worldText}"></label>
// 3. Using the pipe (|) character
<label th:text="|Hello ${worldText}|"></label>
// 4. Using expression inlining
<label>Hello [[${worldText}]]</label>
// 5. You could prepare a variable in your controller
// In contoller,
model.addAttribute("helloWorld", "Hello " + worldText);
// In template,
<label th:text="${helloWorld}"></label>
结果将永远是
<label>Hello variable-value</label>