【问题标题】:Thymeleaf construct URL with variableThymeleaf 构造带有变量的 URL
【发布时间】:2013-02-03 00:06:02
【问题描述】:

我有以下代码在我的控制器中设置一个变量:

model.set("type", type);

在 thymeleaf 视图中,我想构造一个带有操作 url 的表单:

/mycontroller/{type}

任何想法如何实现这一目标?我已经阅读了 thymeleaf 文档,但没有运气。

【问题讨论】:

    标签: java jsp thymeleaf


    【解决方案1】:

    user482745 suggests in the comments(现已删除),我之前建议的字符串拼接

    <form th:action="@{/mycontroller/} + ${type}">
    

    在某些网络环境中会失败。

    Thymeleaf 使用解析@{..} 表达式的LinkExpression。在内部,它使用HttpServletResponse#encodeURL(String)。它的 javadoc 状态

    对于强大的会话跟踪,servlet 发出的所有 URL 都应该是 通过这个方法运行。否则,URL 重写不能与 不支持 cookie 的浏览器。

    在通过 URL 完成会话跟踪的 Web 应用程序中,该部分将在附加 ${..} 之前附加到为 @{..} 发出的字符串中。你不想要这个。

    改为使用documentation中建议的路径变量

    还可以包含路径变量形式的参数 类似于普通参数,但在内部指定一个占位符 您的网址路径:

    <a th:href="@{/order/{id}/details(id=3,action='show_all')}">
    

    所以你的例子看起来像

    <form th:action="@{/mycontroller/{path}(path=${type})}"> //adding ending curly brace
    

    【讨论】:

      【解决方案2】:

      如果您不想使用字符串连接(proposed by Sotirios),可以在URL link 中使用expression preprocessing

      <form th:action="@{/mycontroller/__${type}__}">
      

      【讨论】:

      • 我也认为这比 Sotirios 提出的解决方案更好。我有th:action="@{/offers/__${offer.id}__/changeStatus}"之类的链接。
      • 这行得通,但您应该知道,这绕过了 Thymeleaf 在使用 Sotirios 回答中的解决方案时所做的路径变量的 URL 编码。
      【解决方案3】:

      您需要在@{} 中连接字符串。

      <form th:action="@{'/mycontroller/' + ${type}}">
      

      @{} 用于 URL 重写。 URL 重写的一部分是跟踪会话。第一次用户请求URL,应用服务器添加到url;jsessionid=somehexvalue并生成带有jsessionid的cookie。当客户端在下一次请求期间发送 cookie 时,服务器知道客户端支持 cookie。如果服务器知道客户端支持cookie,服务器将不会在URL中保留addind jsessionid。

      我首选的方法是使用管道语法 (|) 进行文字替换。

      <form th:action="@{|/mycontroller/${type}|}">
      

      Thymeleaf 路径变量语法是

      <form th:action="@{/mycontroller/{pathParam}(pathParam=${type}}">
      

      参考: Thymeleaf Standard URL Syntax

      【讨论】:

      • 我也更喜欢文字替换,因为它更清晰且易于维护。 thymeleaf 语法​​对于简单的任务来说太复杂了。
      • 进行字符串连接对我有用,但这很丑陋。为什么写th:href="@{/foo/bar/{obj.property}}" 不起作用?这对我不起作用。
      • 这里的 Thymeleaf 版本运行良好,但缺少正确的括号。应该是:&lt;form th:action="@{/mycontroller/{pathParam}(pathParam=${type})}"&gt;
      【解决方案4】:

      你需要的是:

      <a th:href="@{/mycontroller/{type}(type=${type})}">
      

      文档:

      这里有很大的帮助: http://www.thymeleaf.org/doc/articles/standardurlsyntax.html。我用了什么 从那里开始:

      还可以包含路径变量形式的参数 类似于普通参数,但在内部指定一个占位符 您的网址路径:

      &lt;a th:href="@{/order/{id}/details(id=3,action='show_all')}"&gt;

      ... 更重要的是:一个 URL 表达式,例如:

      &lt;a th:href="@{/order/details(id=${order.id})}"&gt;

      【讨论】:

      • 有一点需要提一下,参数需要全部在链接表达式的末尾。这是不正确的语法:@{/order/{id}(id=${anIdExpression})/detail/{detail}(detail=${aDetailExpression)} 但这是正确的:@{/order/{id}/detail/{detail}(id=${anIdExpression}, detail=${aDetailExpression)} 文档在这一点上不是很清楚,所以我想我会节省其他人花费在反复试验上的时间。
      【解决方案5】:
      Exception evaluating SpringEL expression: "businessId" (login:50)
      

      我有同样的问题,通过下面的字符串连接解决。

      LoginController.java

      @RequestMapping(value = "/login/{businessId}", method = RequestMethod.GET)
          public ModelAndView get(HttpServletRequest request, @PathVariable String businessId) {
              ModelAndView modelAndView = new ModelAndView("login");
              modelAndView.addObject("businessId", businessId);
              return modelAndView;
          }
      

      login.html

                  <form role="form" th:action="@{/login} + '/'+ ${businessId}" th:method="post">
                                  <fieldset>
                                      <div class="form-group">
                                          <input class="form-control" placeholder="E-mail" name="userName"
                                              type="email"></input>
                                      </div>
                                      <div class="form-group">
                                          <input class="form-control" placeholder="Password"
                                              name="password" type="password" value=""></input>
                                      </div>
                                      <div class="checkbox">
                                          <label> <input name="remember" type="checkbox"
                                              value="Remember Me"></input>Remember Me
                                          </label>
                                      </div>
                                      <!-- Change this to a button or input when using this as a form -->
                                      <button id="login" class="btn btn-lg btn-success btn-block" type="submit">Login</button>
                                  </fieldset>
                  </form>
      

      【讨论】:

        猜你喜欢
        • 2017-12-01
        • 2016-02-28
        • 2013-08-21
        • 2017-04-12
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        相关资源
        最近更新 更多