1. Problem
We want to build a link that starts with a context path in Thymeleaf view. Additionally, we want to create this link in JavaScript.
2. Solution
To create a Context-relative URLs we need to use @
in th:href
attribute like in the following example:
<a th:href="@{/test}">This is a test link</a>
if application was installed under http://localhost:8080/sampleapp
that URL will look like the following:
<a href="/sampleapp/test">This is a test link</a>
Read more about how to create links in Thymeleaf here.
In case you need to define an URL in JavaScript code:
<script th:inline="javascript">
var link = /*[[@{/test}]]*/ 'test';
document.getElementById("user_link").href = link;
</script>
The /*[[ - ]]*/
syntax is used by Thymeleaf to evaluate variables used in JavaScript.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}