2

So I'm trying to generate (dynamic) links through Thymeleaf.

Now the first url I want to link to is /resource/id so I have the following:

<a th:href="|/resource/${resource.id}|" href="#" class="btn btn-default">Edit...</a>

Which works great, and does exactly what you would expect.

So the next url I want to link to is /resource/id/child/id so I try the following:

<a th:href="|/resource/${resource.id}/child/${child.id}|" href="#" class="btn btn-default">Edit...</a>

Which I thought would give me what I wanted. Instead I get:

/resource/3/child%7D?id=4

Which is weird and confusing. I'm not really sure why this happens or how to get what I want which is:

/resource/3/child/4

Any help greatly appreciated. I'm using spring-boot/spring-mvc if it makes any difference.

Update: Okay, so after re-reading (and comprehending) the documentation, it turns out the the more correct syntax is:

<a th:href="@{/resource/{rid}/child/{cid}(rid=${resource.id} cid=${child.id})}" href="#" class="btn btn-default">Edit...</a>

Now I get the url:

/resource/3/child?id=4

Which while very close is still slightly wrong and confusing.

Does Thymeleaf not support more than one path variable?

3 Answers 3

2

I think a comma is missing. Try like this :

th:href="@{/resource/{rid}/child/{cid}(rid=${resource.id},cid=${child.id})}"

Each key/value should be separate with a comma in order to correctly build an URL with multiple path variables.

Sign up to request clarification or add additional context in comments.

Comments

0

When working with urls you should better use the url syntax @{...} instead of literal substitution. For example in your case:

    <a th:href="@{/resource/{resource.id}/child/{child.id}}" 
          href="#" class="btn btn-default">Edit...</a>

(note the absence of the dollar sign in rest resource placeholders)

2 Comments

I tried this originally, but that just gives me a URL that says /resource/%7Bresource,id%7D, and the second url is /resource/%7Bresource.id%7D/child?id=4, which is even more wrong. :(
see answer by heRoy. I forgot about the parameters mapping
0

Have you check the value of child.id? I used it very often like @{|/resource/${resource.id}/child/${child.id}|}. You can evaluate the syntax wit <a th:with="child_id='4'" th:href="@{|/resource/${resource.id}/child/${child_id}|}" and put a <span th:text="${child.id}"></span> to your html to see what value child.id has.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.