1
votes

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!

I think you are using too many brackets.Elmar Brauch
I don't believe the Thymeleaf @{...} syntax supports URL fragments (meaning a # followed by a fragment identifier). It only supports path segments and query parameters. Therefore the only way I know to achieve this is with a string expression, for example <a th:href="${'import#' + object.id}">link</a> or any equivalent string variation. Leaving this as a comment in case someone proves me wrong.andrewjames
By the way, your expression in the question is missing the closing ) in ...(id=${object.id})} - but that still does not evaluate to what you need.andrewjames
Actually, I think a much better example would be something like <a th:href="@{import} + ${'#' + object_id}">link</a>. This still relies on string concatenation, but you also are still using @{...} and can therefore still take advantage of its features (such as handling for absolute and relative URLs).andrewjames