如何在Thymeleaf中对变量进行子字符串化并将其与url连接

问题描述

我想对作为我的JPA类ID字段的变量进行子字符串化,然后将其添加到Thymeleaf中的URL。

我的网址就像

/Myapplication/sortddoc/value=__${entity.id}__

所以我需要做类似的事情

${entity.id}.substr(0,8) 

将其连接到URL之前。

我试图在我的实体类中创建一个包含substring变量的新Transient字段,但是它不起作用,因为它似乎需要我无法提供的数据库字段。

有人可以帮助我吗?

解决方法

您应该使用Thymeleaf的standard URL syntax来做到这一点,而不是连接字符串变量或使用预处理(不需要任何一个)。

<a th:with="${value=#strings.substring(entity.id,8)}"
   th:href="@{/Myapplication/sortddoc/(value=${value})}"></a>

<a th:href="@{/Myapplication/sortddoc/(value=${#strings.substring(entity.id,8)})}"></a>