Vy Do
Vy Do

Reputation: 52516

Thymeleaf 2 expressions in URL

I am using Spring Boot 2.1.6.RELEASE , Thymeleaf 3.0.11.RELEASE . I have

<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>

It works ok, generate to http://localhost:8080/customer/42. I try

<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>

It does not work as expected, I want it generate to http://localhost:8080/customer/1/42 (accountObject.accountObjectType = 2)

I need something like /customer/1/ because it will be @PathVariable("type") Integer type in Spring MVC Controller.

How to put 2 expressions in Thymeleaf URL?

Upvotes: 1

Views: 80

Answers (1)

You can have multiple placeholders in a URI template; provide all the substitutions at once at the end. It should look something like this:

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

(I also think you probably don't need the {} for each substitution value, but it won't hurt.)

Upvotes: 1

Related Questions