我在
http://example.com/some/page?p1=11
我想在不重新定义当前网址的情况下添加一个参数:
http://example.com/some/page?p1=11&p2=32
类似这样:
<a th:href="@{?(p2=32)}">Click here</a>
但是上面的代码返回 http://example.com/some/page?&p2=32
(移除了p1
参数)。
我该如何使用Thymeleaf实现这个功能?
我在
http://example.com/some/page?p1=11
我想在不重新定义当前网址的情况下添加一个参数:
http://example.com/some/page?p1=11&p2=32
类似这样:
<a th:href="@{?(p2=32)}">Click here</a>
但是上面的代码返回 http://example.com/some/page?&p2=32
(移除了p1
参数)。
我该如何使用Thymeleaf实现这个功能?
<span th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
th:text="${urlBuilder.replaceQueryParam('p2', '32').toUriString()}">
</span>
http://example.com/some/page?p1=11
,输出如下:http://example.com/some/page?p1=11&p2=32
解释:
T
操作符 来访问 ServletUriComponentsBuilder
类型。fromCurrentRequest
创建的实例被保存到 urlBuilder
变量中。replaceQueryParam
方法在查询字符串中添加或替换参数,然后构建URL。优点:
?
。缺点:
!请注意,上述解决方案创建了一个构建器实例。这意味着该构建器不能被重用,因为它仍然修改原始URL。对于页面上的多个URL,您必须创建多个构建器,如下所示:
<span th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder)}">
<span th:text="${urlBuilder.fromCurrentRequest().replaceQueryParam('p2', 'whatever').toUriString()}"></span>
<span th:text="${urlBuilder.fromCurrentRequest().replaceQueryParam('p3', 'whatever').toUriString()}"></span>
<span th:text="${urlBuilder.fromCurrentRequest().replaceQueryParam('p4', 'whatever').toUriString()}"></span>
</span>
对于http://example.com/some/page
,输出如下:
http://example.com/some/page?p2=whatever
http://example.com/some/page?p3=whatever
http://example.com/some/page?p4=whatever
replaceQueryParam
,您可以像这样使用 ${urlBuilder.replaceQueryParam('page', current).replaceQueryParam('size', page.size).toUriString()
。对于未来的任何人来说,这都是适用的。 - Rahmat Nazali Salimi.build().toUriString()
来避免p=%25
再次被编码为p=%2525
。 - bitdancer<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
http://localhost:8080/some-page?param1=1&myparam=test&myparam=test
编辑:
这里有一个解决方法,可以从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();
}
${#httpServletRequest.requestURI + '?' + #strings.defaultString(#httpServletRequest.queryString, '')}
,以避免将null
附加到您的URL中。 - Priidu Neemreth:href="@{/your/link?parameter=__${appendParameter}__}"
__
(双下划线)才能运行,这些不会出现在结果中。 - Hans Kestingth:href="@{http://example.com/some/page(p1=11,p2=32)}"
th:href="@{http://example.com/some/page(p1=11,p2=${someid})}"
@Bean
public BiFunction<String, String, String> replaceOrAddParam() {
return (paramName, newValue) -> ServletUriComponentsBuilder.fromCurrentRequest()
.replaceQueryParam(paramName, newValue)
.toUriString();
}
<a th:href="${@replaceOrAddParam.apply('myParamName', 'myNewValue')}">Text</a>
这将适用于您,即使在Unicode中:
<ul class="pagination">
<li th:if="${currentPage > 1}"><a th:href="'/search?key=' + ${param.key[0]} + '&page=' + ${currentPage-1}">Previous</a></li>
<li th:each="i : ${#numbers.sequence( 1, total+1)}" th:class="${i==currentPage}?active:''">
<a th:href="'/search?key=' + ${param.key[0]} + '&page=' + ${i}" th:inline="text">
[[${i}]] <span class="sr-only">(current)</span>
</a>
</li>
<li><a th:if="${total + 1 > currentPage}" th:href="'/search?key=' + ${param.key[0]} + '&page=' + ${currentPage+1}">Next</a></li>
</ul>