And*_*rea 15 java spring-mvc url-parameters thymeleaf spring-boot
我在
http://example.com/some/page?p1=11
Run Code Online (Sandbox Code Playgroud)
我想在当前网址中添加一个参数,而不必重新定义它:
http://example.com/some/page?p1=11&p2=32
Run Code Online (Sandbox Code Playgroud)
有类似的东西:
<a th:href="@{?(p2=32)}">Click here</a>
Run Code Online (Sandbox Code Playgroud)
但上面的代码返回http://example.com/some/page?&p2=32(删除p1参数).
我怎么能用Thymeleaf做到这一点?
Raf*_*Raf 14
最简单的解决方案是连接"requestURI"和"queryString".这是一个例子:
<div th:with="currentUrl=(${#httpServletRequest.requestURI + '?' + #strings.defaultString(#httpServletRequest.queryString, '')})">
<a th:href="@{${currentUrl}(myparam=test)}">click here</a>
</div>
Run Code Online (Sandbox Code Playgroud)
" http:// localhost:8080/some-page?param1 = 1 "的结果:
http://localhost:8080/some-page?param1=1&myparam=test
Run Code Online (Sandbox Code Playgroud)
" http:// localhost:8080/some-page "的结果:
http://localhost:8080/some-page?&myparam=test
Run Code Online (Sandbox Code Playgroud)
缺点:
Thymeleaf不会覆盖参数 - 只会向URL添加参数.因此,如果用户再次单击该URL,结果将是:
http://localhost:8080/some-page?param1=1&myparam=test&myparam=test
Run Code Online (Sandbox Code Playgroud)
参考文献: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>
Run Code Online (Sandbox Code Playgroud)
接下来在Spring配置中:
@Bean
public Function<String, String> currentUrlWithoutParam() {
return param -> ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam(param).toUriString();
}
Run Code Online (Sandbox Code Playgroud)
对于更"全局"的解决方案,我会尝试为属性"th:href"扩展处理器或创建自己的属性.我不是一个百里香专家,只是面临类似的问题.
Vác*_*žel 14
您可以直接从Thymeleaf使用URI构建器.
<span th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
th:text="${urlBuilder.replaceQueryParam('p2', '32').toUriString()}">
</span>
Run Code Online (Sandbox Code Playgroud)
对于URL http://example.com/some/page?p1=11打印输出:
http://example.com/some/page?p1=11&p2=32
Run Code Online (Sandbox Code Playgroud)
解释:
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>
Run Code Online (Sandbox Code Playgroud)
对于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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12019 次 |
| 最近记录: |