Java Thymeleaf 使用路径变量到 th:href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33753975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:01:11  来源:igfitidea点击:

Thymeleaf using path variables to th:href

javaspringspring-mvcspring-bootspring-el

提问by user962206

Here's my code, where I'm iterating through:

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

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

The URL it points to is supposed to be /category/edit/<id of the category>, but it says it could not parse the expression:

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

Exception evaluating SpringEL expression: "category.id" (category-list:21)

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

回答by Eddie Jaoude

Your code looks syntactically correct, but I think your property doesn't exist to create the URL.

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

I just tested it, and it works fine for me.

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

Try using category.idCategoryinstead of category.id, for example…

尝试使用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>

回答by cralfaro

I think your problem was a typo:

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

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

You are using category.id, but in your code is idCategory, as Eddie already pointed out.

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

This would work for you:

这对你有用:

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

回答by Denis Marennikov

I think you can try this:

我想你可以试试这个:

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

Or if you have "idCategory" this:

或者,如果您有“idCategory”:

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

回答by douglasmiguel7

A cleaner and easier way to do this

一种更干净、更简单的方法来做到这一点

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

I found this solution in Thymeleaf Documentationon "4.8 Literal substitutions".

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

回答by Mario Rojas

The right way according to Thymeleaf documention for adding parametersis:

根据Thymeleaf 文档添加参数的正确方法是:

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

回答by Shahriar

I was trying to go through a list of objects, display them as rows in a table, with each row being a link. This worked for me. Hope it helps.

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

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

回答by Shanmukh Sravanth

"List" is an object getting from backend and using iterator to display in table

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

"minAmount" , "MaxAmount" is an object variable "mrr" is an just temporary var to get value and iterate mrr to get data.

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

回答by Ja'afar Naddaf

This is the correct way to add the URL: @{${'/category/edit/' + category.id}}

这是添加 URL 的正确方法:@{${'/category/edit/' + category.id}}

回答by Enamul Haque

You can use like

你可以使用像

  1. My table is bellow like..

    <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. Here is my controller ..

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