thymeleaf - 如何将片段传递给Thymeleaf中的消息表达式

标签 thymeleaf

是否可以将片段传递到Thymeleaf中的消息表达式?我想重复使用片段在消息中创建链接。

我的片段看起来像这样:

<div th:fragment="link(url, text)" th:remove="tag">
   <a th:href="@{${url}}"><span>${text}</span></a>
</div>

我有这样的消息:

home.welcome=Hello User! See new content at {0}. 

现在,我想将评估后的片段传递给消息表达式(伪代码):

<p th:utext="#{home.welcome(${link:: link(url='myUrl', text='myText')})}"></p>

产生的HTML应该如下所示:

<p>
    Hello User! See new content at <a href="myUrl"><span>myText</span></a>.
</p>

我发现Thymeleaf 3中引入了Fragment expressions,但是我不确定它们是否可行。

最佳答案

您可以使用尝试 th:。

像这样调用片段

<div th:include="fragments/common :: link" th:with="url='www.google.com', text='Click Me'"></div>

这是你的片段
<div th:fragment="link" th:remove="tag">
    <a th:href="@{${url}}"><span th:inline="text">[[${text}]]</span></a>
</div>

这是您将获得的HTML
<div>
    <a href="www.google.com">
        <span>Click Me</span>
    </a>
</div>

关于thymeleaf - 如何将片段传递给Thymeleaf中的消息表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023365/

相关文章:

java - Spring 安全角色层次结构不适用于 Thymeleaf sec :authorize

java - 使用什么类型的对象来制作带有选择字段的表单?

java - 使用 Spring Boot 处理单个 Thymeleaf 片段

spring - Thymeleaf th :inline ="javascript" issue

java - Spring boot - Thymeleaf 与 JQuery?

spring - Thymeleaf - 上下文相关和斜杠结尾 url

jsp - JavaScript 中的 Thymeleaf forEach 循环

java - 模板解析时发生错误(模板: "class path resource [templates/addWunsch.html]")

spring-security - 如何使用秒:[something] in a thymeleaf expression?

java - 导入数据时,thymeleaf 和 ajax 有什么区别?