简体   繁体   English

如何使用 thymeleaf th:each 向表的列添加超链接?

[英]How can I add hyperlinks to columns of a table using thymeleaf th:each?

I'm trying to create hyperlinks for each element in a table generated by thymeleaf.我正在尝试为 thymeleaf 生成的表中的每个元素创建超链接。

<tbody>
    <tr th:each="author :${authors}">
        <td th:text="${author.id}"></td>
        <a th:href="@{/authors/{id}(id=${author.id})}">
            <td class="authorLink" th:text="${author.firstName}"></td>
        </a>
        <td th:text="${author.lastName}"></td>
    </tr>
</tbody>

The code above is giving me an output of the generated hyperlinks outside of the table.上面的代码给了我一个表外生成的超链接的 output。

This is a link to the generated html.这是生成的 html 的链接。 https://i.gyazo.com/7dae68eb42cd084b59030e7b17590e5e.png https://i.gyazo.com/7dae68eb42cd084b59030e7b17590e5e.png

"linklinklinklink" is the output of the generated hyperlinks. “linklinklink”是生成的超链接的output。 I would like for the 'First Name' column to become hyperlinks.我希望“名字”列成为超链接。 If anyone can tell me how I can accomplish this that would be great.如果有人能告诉我如何做到这一点,那就太好了。

Place your <a> tag inside the <td> cell where you want the link to appear:将您的<a>标签放在您希望链接出现的<td>单元格中:

<td>
    <a th:href="@{/authors/{id}(id=${author.id})}" 
       th:text="${author.id}"></a>
</td>

Note how you can use a th:text="..." attribute inside the <a> tag, as well, to control the visible text for the link.请注意如何在<a>标记内使用th:text="..."属性来控制链接的可见文本。

In your case, you had a <a> tag inside a row, but not part of any cell.在您的情况下,您在一行中有一个<a>标记,但不是任何单元格的一部分。 This is invalid HTML, so your browser's HTML renderer dumped the links somewhere else (above the table, in this case).这是无效的 HTML,因此您的浏览器的 HTML 渲染器将链接转储到其他地方(在这种情况下,在表格上方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM