Java 如何使用传递给模板的参数在thymeleaf中创建URL?

Java 如何使用传递给模板的参数在thymeleaf中创建URL?,java,spring,spring-boot,templates,thymeleaf,Java,Spring,Spring Boot,Templates,Thymeleaf,我已经为电子邮件创建了一个html模板 现在,我在上下文中加入一个变量: context.setVariable("invoiceId", invoiceId); 在模板中,我有一个标记: <p><span>To accept the invoice, click <a th:href="http://localhost/accept/${invoiceId}">HERE</a></span></p> 在这种情况下,如何

我已经为电子邮件创建了一个html模板

现在,我在上下文中加入一个变量:

context.setVariable("invoiceId", invoiceId);
在模板中,我有一个标记:

<p><span>To accept the invoice, click <a th:href="http://localhost/accept/${invoiceId}">HERE</a></span></p>

在这种情况下,如何使用${invoiceId}变量?

通常,您使用模型与控制器和视图进行通信(我假设您使用Spring MVC,因为您已经在使用Spring Boot)。这里唯一的区别是

 model.addAttribute("invoiceId", invoiceId); 
无论您如何传达信息,在生成URL时都应该使用。它们以
@
开头,通常允许您的应用程序移动,而无需在任何地方硬编码地址:

 <a th:href="@{/accept/{id}(id=${invoiceId})}">link text</a>


请注意thymeleaf是如何处理这些参数的:您可以在url模板中使用占位符,例如
{foo}
{bar}
,然后在最后解释它们的含义,例如
(foo=${baz},bar=${qux}
,其中
${}中的表达式的内容
可以是thymeleaf可以解释的任何内容。

我解决了部分问题,但问题是-我正在生成一个电子邮件模板,当我使用:
时,我只得到
接受/5d00d36b2f8c3230d23d388a
作为链接,
http://localhost
无处可寻:/Follow@tucuxi的答案<代码>
 <a th:href="@{/accept/{id}(id=${invoiceId})}">link text</a>