Home > Mobile >  Thymeleaf Link url expression with anchor
Thymeleaf Link url expression with anchor

Time:09-25

I need create a url in thymeleaf with anchor. Like:

/import#34546

But the link url expression does not work.

<a th:href="@{import#{id}(id=${object.id}}">link</a>

someone get this url using thymeleaf?

I use thymeleaf with spring-boot 2.5.4.

Thanks!

CodePudding user response:

Summary

Thymeleaf does not support the use of expressions for evaluating URL fragments. It assumes the fragment will be hard-coded.

Therefore I suggest you use a combination of the @{...} syntax for everything apart from the fragment, and then use string concatenation to build the fragment with a Thymeleaf ${...} expression.

One example (there are other ways to do concatenation, also):

<a th:href="@{import}   ${'#'   object_id}">link</a>

This allows you to still take advantage of the features of @{...}, such as handling for absolute and relative URLs.

Details

Looking at the Thymeleaf source code for the StandardLinkBuilder class, we can see how URL fragments (the # and its following fragment identifier) are handled.

Points to note are:

The fragment portion of the URL is extracted here:

urlFragment = linkBase.substring(hashPosition);

The hash position is the location of a # in the URL string.

This substring is then removed from the URL string:

linkBase.delete(hashPosition, linkBase.length());

Later on (see here), after all other parameters have been processed, the urlFragment is added back to the URL, unchanged:

linkBase.append(urlFragment);

So, there is no processing of Thymeleaf expression variables for the URL fragment - it is just treated as a "hard-coded" string.

This means you can do something like this, where #foo is hardcoded:

<a th:href="@{import/{id}#foo(id=${object_id})}">link</a>

But you cannot do this (and get the desired result):

<a th:href="@{import/{id_a}#{id_b}(id_a=${object_id},id_b=${object_id})}">link</a>
  • Related