【问题标题】:How to substring variable and concatenate it with url in Thymeleaf如何在 Thymeleaf 中对变量进行子字符串化并将其与 url 连接起来
【发布时间】:2020-12-08 21:36:33
【问题描述】:

我想将一个变量作为我的 JPA 类 id 字段的子字符串,然后将其添加到 Thymeleaf 中的 URL。

我的网址是这样的

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

所以我需要做类似的事情

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

在将其连接到 URL 之前。

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

有人可以帮帮我吗?

【问题讨论】:

  • 这是您的网址:/Myapplication/sortddoc/value=?你确定吗?应该是这样的:/Myapplication/sortddoc?value=?您的 value 参数是路径的一部分还是路径的一部分?
  • 可能两者兼有,在 thymeleaf 中我将外部 url 称为 th:href="@{/Myapplication/sortddoc/value=__${entity.id}__} 但我可以做到就像你说的,我的问题是如何在将 ${entity.id} 添加为值之前对其进行子串化
  • 有Thymeleaf #strings辅助对象;这样您就可以创建临时变量并在路径中使用它:th:with="${trancatedId=#strings.substring(entity.id,0,8)}"
  • 这对我很有效,非常感谢

标签: javascript spring-boot jpa thymeleaf


【解决方案1】:

您应该使用 Thymeleaf 的 standard URL syntax 执行此操作,而不是连接字符串变量或使用预处理(不需要其中任何一个)。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    相关资源
    最近更新 更多