B
B
Biaci_Anj2022-01-27 22:37:13
Java
Biaci_Anj, 2022-01-27 22:37:13

Thymeleaf, how to add a default value for a parameter if it's missing when a link is clicked?

Please tell me how using Thymeleaf. add a parameter to a link with a default value only if it's not already in the link?

This is how I add a new parameter, but this link must also add the sortDir and sortField parameters with default values ​​if they are missing .

<ul th:with="urlBuilder=
${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
    <li><a th:href="@{${urlBuilder.replaceQueryParam('category', 'vehicles').build().toUriString()}}"
               class="dropdown-item" href="#" >Show all</a></li>


I would be very grateful for the help

PS this is necessary, because otherwise in the controller I will have to handle 3 situations:
1. When only the category parameter
2. When the category && sortDir sortField
3. When only the parameters sortDir sortField Was wondering

if it was possible to do this to eliminate the situation when the category parameter is one, adding missing ones.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-01-27
@Biaci_Anj

Thymeleaf, how to add a default value for a parameter if it's missing when a link is clicked?

No way. thymeleaf is on the server side. And clicking on the link occurs on the client side (in the browser), where there is no thymeleaf.
Accordingly, you need to validate using js || jQuery.
Relatively speaking, you broadcast a handler on a click on a link
Google jQuery .on('click', function(){})or .click()
If there is no necessary parameter, then add it using js and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question