繁体   English   中英

Thymeleaf:<label>将动态文本与静态文本 Spring MVC 连接起来</label>

[英]Thymeleaf: <label> to have dynamic text concatenated with static text Spring MVC

我正在尝试将一些文本附加到动态文本,如下所示:

<label th:text="Hello ${worldText}"></label>

但是用户界面抛出:

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM