最简单的解决方案是连接“requestURI”和“queryString”。
这是一个例子:
<div th:with="currentUrl=(${#httpServletRequest.requestURI + '?' + #strings.defaultString(#httpServletRequest.queryString, '')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
“http://localhost:8080/some-page?param1=1”的结果:
http://localhost:8080/some-page?param1=1&myparam=test
“http://localhost:8080/some-page”的结果:
http://localhost:8080/some-page?&myparam=test
缺点:
Thymeleaf 不会覆盖参数 - 只会将参数添加到 URL。因此,如果您用户再次单击该 URL,结果将是:
http://localhost:8080/some-page?param1=1&myparam=test&myparam=test
参考:
http://forum.thymeleaf.org/How-to-link-to-current-page-and-exchange-parameter-td4024870.html
编辑:
这里有一些解决方法,从 URL 中删除参数“myparam”:
<div th:with="currentUrl=(${@currentUrlWithoutParam.apply('myparam')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Spring 配置中的下一步:
@Bean
public Function<String, String> currentUrlWithoutParam() {
return param -> ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam(param).toUriString();
}
对于更多“全局”解决方案,我会尝试为属性“th:href”扩展处理器或创建我自己的属性。我不是百里香专家,只是面临类似的问题。