【问题标题】:Thymeleaf using path variables to th:hrefThymeleaf 使用 th:href 的路径变量
【发布时间】:2016-02-18 15:34:40
【问题描述】:

这是我的代码,我正在迭代:

<tr th:each="category : ${categories}">
     <td th:text="${category.idCategory}"></td>
     <td th:text="${category.name}"></td>
     <td>
         <a th:href="@{'/category/edit/' + ${category.id}}">view</a>
     </td>
</tr>

它指向的 URL 应该是 /category/edit/&lt;id of the category&gt;,但它说它无法解析表达式:

评估 SpringEL 表达式的异常:“category.id” (类别列表:21)

【问题讨论】:

  • 您确定id 是类别对象的属性吗?还是idCategory 语义不同?
  • idCategory 和 id 有什么区别?
  • 你有剩余的堆栈跟踪吗?

标签: java spring spring-mvc spring-boot spring-el


【解决方案1】:

根据Thymeleaf documention for adding parameters的正确做法是:

<a th:href="@{/category/edit/{id}(id=${category.idCategory})}">view</a>

【讨论】:

  • 这是一个被低估的答案。在我看来,这是最好的。
【解决方案2】:

我认为你的问题是一个错字:

<a th:href="@{'/category/edit/' + ${category.id}}">view</a>

您正在使用category.id,但在您的代码中是 idCategory,正如 Eddie 已经指出的那样。

这对你有用:

<a th:href="@{'/category/edit/' + ${category.idCategory}}">view</a>

【讨论】:

    【解决方案3】:

    一种更清洁、更简单的方法

    <a href="somepage.html" th:href="@{|/my/url/${variable}|}">A Link</a>
    

    我在Thymeleaf Documentation 的“4.8 文字替换”中找到了这个解决方案。

    【讨论】:

      【解决方案4】:

      您的代码看起来语法正确,但我认为您的属性不存在用于创建 URL。

      我刚刚测试了它,它对我来说很好用。

      尝试使用category.idCategory 代替category.id,例如...

        <tr th:each="category : ${categories}">
          <td th:text="${category.idCategory}"></td>
          <td th:text="${category.name}"></td>
          <td>
            <a th:href="@{'/category/edit/' + ${category.idCategory}}">view</a>
          </td>
        </tr>
      

      【讨论】:

        【解决方案5】:

        我试图浏览一个对象列表,将它们显示为表格中的行,每一行都是一个链接。这对我有用。希望对您有所帮助。

        // CUSTOMER_LIST is a model attribute
        <table>
            <th:block th:each="customer : ${CUSTOMER_LIST}">
                <tr>
                    <td><a th:href="@{'/main?id=' + ${customer.id}}" th:text="${customer.fullName}" /></td>
                </tr>
            </th:block>
        </table>
        

        【讨论】:

          【解决方案6】:

          我想你可以试试这个:

          <a th:href="${'/category/edit/' + {category.id}}">view</a>
          

          或者如果你有“idCategory”这个:

          <a th:href="${'/category/edit/' + {category.idCategory}}">view</a>
          

          【讨论】:

            【解决方案7】:

            你可以用like

            1. 我的桌子像下面这样..

              <table>
                 <thead>
                  <tr>
                      <th>Details</th>
                  </tr>
              </thead>
              <tbody>
                  <tr th:each="user: ${staffList}">
                      <td><a th:href="@{'/details-view/'+ ${user.userId}}">Details</a></td>
                  </tr>
               </tbody>
              </table>
              
            2. 这是我的控制器..

              @GetMapping(value = "/details-view/{userId}")
              public String details(@PathVariable String userId) { 
              
                  Logger.getLogger(getClass().getName()).info("userId-->" + userId);
              
               return "user-details";
              }
              

            【讨论】:

            【解决方案8】:

            “列表”是从后端获取并使用迭代器显示在表格中的对象

            "minAmount" , "MaxAmount" 是一个对象变量 "mrr" 只是一个临时变量,用于获取值并迭代 mrr 以获取数据。

            <table class="table table-hover">
            <tbody>
            <tr th:each="mrr,iterStat : ${list}">
                    <td th:text="${mrr.id}"></td>
                    <td th:text="${mrr.minAmount}"></td>
                    <td th:text="${mrr.maxAmount}"></td>
            </tr>
            </tbody>
            </table>
            

            【讨论】:

              【解决方案9】:

              你可以使用:

              html:

               <html xmlns:th="http://www.thymeleaf.org">
              
              <a th:href="@{'/category/'+ ${item.idCategory}}">View</i></a>
              

              控制器:@PathVariable String parentId

              【讨论】:

              【解决方案10】:

              如果您在模型 foreach(Modal 中的 var 项)循环列表中,试试这个是一种非常简单的方法

              &lt;th:href="/category/edit/@item.idCategory&gt;View&lt;/a&gt;"

              &lt;th:href="/category/edit/@item.idCategory"&gt;View&lt;/a&gt;

              【讨论】:

                猜你喜欢
                • 2016-05-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-12-07
                • 2023-03-31
                • 2018-05-06
                • 2020-06-02
                • 2013-05-15
                相关资源
                最近更新 更多