有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Thymeleaf:将参数添加到当前url

我在

http://example.com/some/page?p1=11

我想在当前url中添加一个参数,而不必重新定义它:

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


共 (1) 个答案

  1. # 1 楼答案

    基于Rafanswer,我最终得到了这个解决方案:

    @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>