subbu royal
subbu royal

Reputation: 624

how to append variable value to th:href in thymeleaf

When I click a link (brandname) in front end, I need send a request to the server using th:href in thymeleaf. For that I followed below code

<a th:href="@{*{category.url}}" th:text=*{brandname}></a>

category.url and brandname values are coming dynamically from their respective Objects, But for above request I need to append extra content for that I tried something like below

<a th:href="@{*{category.url}+'?someExtraContent'}" th:text=*{brandname}></a>

and it is adding the extra content successfully, now my task is that 'extra content' I need to append dynamically, I read Local Variable concept in thymeleaf and I tried below code to append content to the request url dynamically

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+var}" th:text=*{brandname}></a>

I assumed that the var will stores the value and it will append to the request url but it is not happening I am getting the url like below

Ex: localhost:8080/mycompany/mens?var

But I need something like below

Ex: localhost:8080/mycompany/mens?nike

can anyone help me how to append dynamic content to the request url in thymeleaf

Upvotes: 2

Views: 4680

Answers (1)

subbu royal
subbu royal

Reputation: 624

I found solution,

Previous code

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+var}" th:text=*{brandname}></a>

I replaced above code with

<a th:with="var=*{brandname}" th:href="@{*{category.url}+'?'+${var}}" th:text=*{brandname}></a>

Now I am able to append the dynamic content to my request url...

I have changed this var to ${var}

Upvotes: 1

Related Questions