The easiest solution is concatenating "requestURI" and "queryString".
Here is example:
<div th:with="currentUrl=(${#httpServletRequest.requestURI + '?' + #strings.defaultString(#httpServletRequest.queryString, '')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Result for "http://localhost:8080/some-page?param1=1":
http://localhost:8080/some-page?param1=1&myparam=test
Result for "http://localhost:8080/some-page":
http://localhost:8080/some-page?&myparam=test
Drawback:
Thymeleaf doesn't overwrite parameters - only adds parameters to URL. So if you user click once again to that URL, the result will be:
http://localhost:8080/some-page?param1=1&myparam=test&myparam=test
References:
http://forum.thymeleaf.org/How-to-link-to-current-page-and-exchange-parameter-td4024870.html
EDIT:
Here is some workaround, which removes parameter "myparam" from the URL:
<div th:with="currentUrl=(${@currentUrlWithoutParam.apply('myparam')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Next in Spring configuration:
@Bean
public Function<String, String> currentUrlWithoutParam() {
return param -> ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam(param).toUriString();
}
For more "global" solution, I would try extending processor for attribute "th:href" or creating my own attribute. I'm not a thymeleaf expert, just facing similar problem.