Thymeleaf:将参数添加到当前网址

时间:2014-12-23 15:32:42

标签: java spring-mvc spring-boot thymeleaf url-parameters

我在

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

进行操作

6 个答案:

答案 0 :(得分:11)

最简单的解决方案是连接“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”扩展处理器或创建自己的属性。我不是一个百里香专家,只是面临类似的问题。

答案 1 :(得分:10)

您可以直接从Thymeleaf使用URI构建器。

<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

<强>解释

  • SpEL T operator用于访问ServletUriComponentsBuilder类型。
  • 由工厂方法fromCurrentRequest创建的实例将保存到urlBuilder变量。
  • 通过replaceQueryParam方法在查询字符串中添加或替换参数,然后构建URL。

<强>优点:

  • 安全解决方案。
  • 如果查询字符串为空,则不会跟踪?
  • Spring上下文中没有多余的bean。

<强>缺点:

  • 非常详细。

!请注意,上面的解决方案会创建一个构建器实例。这意味着构建器无法重用,因为它仍然会修改单个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

答案 2 :(得分:1)

根据docs,您可以指定所有参数

th:href="@{http://example.com/some/page(p1=11,p2=32)}"

您可以使用表达式获取值:

th:href="@{http://example.com/some/page(p1=11,p2=${someid})}"

答案 3 :(得分:0)

这对您有用,即使是在Unicode中:

 <ul class="pagination">
                <li th:if="${currentPage > 1}"><a th:href="'/search?key=' + ${param.key[0]} + '&amp;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]} + '&amp;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]} + '&amp;page=' +   ${currentPage+1}">Next</a></li>
            </ul>

答案 4 :(得分:0)

th:href="@{/your/link?parameter=__${appendParameter}__}"

答案 5 :(得分:0)

基于来自 answerRaf 我最终得到了这个解决方案:

@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>
相关问题