I'm at
我在
http://example.com/some/page?p1=11
and I want to add a parameter to current url without having to redefine it:
我想在当前url中添加一个参数,而不需要重新定义它:
http://example.com/some/page?p1=11&p2=32
with something like:
可以这样说:
<a th:href="@{?(p2=32)}">Click here</a>
but the above code return http://example.com/some/page?&p2=32
(removes the p1
parameter).
但是上面的代码返回http://example.com/some/page?p2=32(删除p1参数)。
How can I do it using Thymeleaf?
我怎么用胸腺叶做呢?
4 个解决方案
#1
10
The easiest solution is concatenating "requestURI" and "queryString". Here is example:
最简单的解决方案是连接“requestURI”和“queryString”。下面是例子:
<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 /某些页面? param1 = 1”:
http://localhost:8080/some-page?param1=1&myparam=test
Result for "http://localhost:8080/some-page":
结果“http://localhost:8080 /某些页面”:
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:
缺点:Thymeleaf不覆盖参数—只向URL添加参数。因此,如果用户再次单击该URL,结果将是:
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
引用: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:
下面是一些变通方法,它从URL中删除参数“parmyam”:
<div th:with="currentUrl=(${@currentUrlWithoutParam.apply('myparam')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Next in Spring configuration:
下一个在Spring配置:
@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.
对于更多的“全局”解决方案,我将尝试扩展处理器的属性“th:href”或创建我自己的属性。我不是thymeleaf专家,只是遇到了类似的问题。
#2
6
You can use URI builder, directly from Thymeleaf.
您可以直接从Thymeleaf使用URI builder。
<span th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
th:text="${urlBuilder.replaceQueryParam('p2', '32').toUriString()}">
</span>
For URL http://example.com/some/page?p1=11
prints out:
在URL http://example.com/some/page?p1 = 11打印出:
http://example.com/some/page?p1=11&p2=32
Explained:
解释道:
- SpEL
T
operator is used for accessingServletUriComponentsBuilder
type. - SpEL T操作符用于访问servleturientsbuilder类型。
- An instance created by factory method
fromCurrentRequest
is saved tourlBuilder
variable. - 工厂方法fromCurrentRequest创建的实例被保存到urlBuilder变量中。
- A param is added or replaced in the query string by
replaceQueryParam
method, and then the URL is built. - 通过replaceQueryParam方法在查询字符串中添加或替换参数,然后构建URL。
Pros:
优点:
- Safe solution.
- 安全解决方案。
- No trailing
?
in case of empty query string. - 不落后?如果查询字符串为空。
- No extra bean in Spring context.
- 在Spring上下文中没有额外的bean。
Cons:
缺点:
- It is quite verbose.
- 很详细。
! Be aware that solution above creates one instance of the builder. This means that the builder cannot be reused because it still modifies a single URL. For multiple URLs on a page you have to create multiple builders, like this:
!请注意,上面的解决方案创建了构建器的一个实例。这意味着构建器不能被重用,因为它仍然修改单个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>
For http://example.com/some/page
prints:
为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
#3
0
This will work for you, even in Unicode:
即使在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>
#4
0
th:href="@{/your/link?parameter=__${appendParameter}__}"
#1
10
The easiest solution is concatenating "requestURI" and "queryString". Here is example:
最简单的解决方案是连接“requestURI”和“queryString”。下面是例子:
<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 /某些页面? param1 = 1”:
http://localhost:8080/some-page?param1=1&myparam=test
Result for "http://localhost:8080/some-page":
结果“http://localhost:8080 /某些页面”:
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:
缺点:Thymeleaf不覆盖参数—只向URL添加参数。因此,如果用户再次单击该URL,结果将是:
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
引用: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:
下面是一些变通方法,它从URL中删除参数“parmyam”:
<div th:with="currentUrl=(${@currentUrlWithoutParam.apply('myparam')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Next in Spring configuration:
下一个在Spring配置:
@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.
对于更多的“全局”解决方案,我将尝试扩展处理器的属性“th:href”或创建我自己的属性。我不是thymeleaf专家,只是遇到了类似的问题。
#2
6
You can use URI builder, directly from Thymeleaf.
您可以直接从Thymeleaf使用URI builder。
<span th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
th:text="${urlBuilder.replaceQueryParam('p2', '32').toUriString()}">
</span>
For URL http://example.com/some/page?p1=11
prints out:
在URL http://example.com/some/page?p1 = 11打印出:
http://example.com/some/page?p1=11&p2=32
Explained:
解释道:
- SpEL
T
operator is used for accessingServletUriComponentsBuilder
type. - SpEL T操作符用于访问servleturientsbuilder类型。
- An instance created by factory method
fromCurrentRequest
is saved tourlBuilder
variable. - 工厂方法fromCurrentRequest创建的实例被保存到urlBuilder变量中。
- A param is added or replaced in the query string by
replaceQueryParam
method, and then the URL is built. - 通过replaceQueryParam方法在查询字符串中添加或替换参数,然后构建URL。
Pros:
优点:
- Safe solution.
- 安全解决方案。
- No trailing
?
in case of empty query string. - 不落后?如果查询字符串为空。
- No extra bean in Spring context.
- 在Spring上下文中没有额外的bean。
Cons:
缺点:
- It is quite verbose.
- 很详细。
! Be aware that solution above creates one instance of the builder. This means that the builder cannot be reused because it still modifies a single URL. For multiple URLs on a page you have to create multiple builders, like this:
!请注意,上面的解决方案创建了构建器的一个实例。这意味着构建器不能被重用,因为它仍然修改单个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>
For http://example.com/some/page
prints:
为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
#3
0
This will work for you, even in Unicode:
即使在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>
#4
0
th:href="@{/your/link?parameter=__${appendParameter}__}"