跳转到其他网页上的 thymeleaf 迭代部分

Jump to thymeleaf-iterated section on other webpage

我试图跳转到另一个页面上的一个部分,该部分是 th:each 循环中的一个条目。

我在一个页面上有一个学生姓名列表,单击它会带您进入一个包含每个用户的更多信息的页面:

<div th:each="student : ${getAllStudents()}">
    <a th:href="@{linkToOtherPage}">[[${student.name}]]</a>
</div>

在另一页上,我有每个用户的额外信息:

<div th:each="student : ${getAllStudents()}">
    <div id="studentInformation">
        <h1>[[${student.name}]]</h1>
        <h3>[[${student.age}]]</h3>
        ...
    </div>
</div>

假设在列表中的第一个位置上有一个叫罗伯特的人,在第 20 个位置上有一个叫桑迪的人。 我需要做什么才能在点击 Sandy 的名字时跳转到 Sandy

我尝试将 #studentInformation 添加到包含学生姓名的页面,所以:

<div th:each="student : ${getAllStudents()}">
    <a th:href="@{linkToOtherPage}#studentInformation">[[${student.name}]]</a>
</div>

但这让我来到了页面顶部。

如果您想要 link 使用 #expression 的部分,该表达式必须是唯一的。如果每个部分都使用 #studentInformation,它不知道 link 到哪里。

如果列表总是按相同的顺序排列,那么这样的方法对您有用:

第一页:

<div th:each="student, status: ${getAllStudents()}">
    <a th:href="|@{linkToOtherPage}#student${status.index}|">[[${student.name}]]</a>
</div>

第二页:

<div th:each="student, status: ${getAllStudents()}">
    <div th:id="|student${status.index}|">
        <h1>[[${student.name}]]</h1>
        <h3>[[${student.age}]]</h3>
        ...
    </div>
</div>

(通过将数组中的索引附加到每个 id,您可以 link 到单个学生。)