java - URL 中的 Thymeleaf 2 表达式

标签 java spring spring-mvc thymeleaf spring-el

我正在使用 Spring Boot 2.1.6.RELEASE 、 Thymeleaf 3.0.11.RELEASE 。 我有

<a class="k-button" th:href="@{/customer/view/{id}(id=${accountObject.id})}" style="min-width: 0; color: green;" title="Xem"><span class="k-icon k-i-preview"></span></a>

它工作正常,生成到http://localhost:8080/customer/42。 我尝试一下

<a class="k-button" th:href="@{/customer/view/{type}(type=${accountObject.accountObjectType})/{id}(id=${accountObject.id})}" style="min-width: 0; color: green;" title="Xem"><span class="k-icon k-i-preview"></span></a>

它没有按预期工作,我希望它生成为 http://localhost:8080/customer/1/42 (accountObject.accountObjectType = 2)

我需要像 /customer/1/ 这样的东西,因为在 Spring MVC Controller 中它将是 @PathVariable("type") Integer type

如何在 Thymeleaf URL 中放置 2 个表达式?

最佳答案

一个 URI 模板中可以有多个占位符;最后一次性提供所有替代品。它应该看起来像这样:

th:href="@{/customer/view/{type}/{id}(type=${accountObject.accountObjectType},id=${accountObject.id})}"

(我还认为您可能不需要每个替换值的 {},但这不会有什么坏处。)

关于java - URL 中的 Thymeleaf 2 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56911290/

相关文章:

java - JSTL foreach - 使用特定字段对列表进行排序

Java 类泛型导致类扩展该类型?

端点方法中的 Spring-ws SoapHeader 字段访问

java - Spring Session 与 Spring Security 创建两个 session

java - 在实现接口(interface)的 Controller 上使用 @Controller 的 Spring-MVC 问题

java - 如何反转已经反转的字符串?

java - 我如何使用 highchart 在 jsp 中的 spring mvc 中绘制图形

java - Gradle 为 Spring 获取错误的库版本

eclipse - 在 Eclipse 上使用 JPA 设置 Spring 3 MVC 项目

java - Jackson 序列化/反序列化 : Dynamic properties and fields