【问题标题】:Spring boot thymeleaf mapping is not working for meSpring Boot thymeleaf 映射对我不起作用
【发布时间】:2021-06-24 07:21:47
【问题描述】:

我知道 stackoverflow 中有一个类似的问题,就像我的一样。我尝试了这个问题的所有答案,但这些都不起作用:Thymeleaf using path variables to th:href

你能帮我在百里香页面上做错什么吗? 我想访问的 URL:localhost:8081/students/{stu_id}

我的 GetMapping 看起来像:

@GetMapping("/students")
    public ModelAndView viewStudentPage() {
        List<Student_Dim> listStudent = studentService.getAllStudents();
        return new ModelAndView("students","students", listStudent);
    }

@GetMapping("/students/{id}")  
    private ModelAndView getInfo(@PathVariable("id") String stu_id){
        Student_Dim student = studentService.getStudentById(stu_id);
        return new ModelAndView("student","student",student);
    } 

我使用的 Href (students.html):

<tbody>
            <tr th:each="student : ${students}">
                <td th:text="${student.stu_fname}">First Name</td>
                <td th:text="${student.stu_lname}">Last Name</td>
                <td th:text="${student.dep_code}">Department Code</td>
                <td th:text="${student.fac_code}">Faculty Code</td>
                <td th:text="${student.loc_code}">Location Code</td>
                <td th:text="${student.marriage_status}">Marriage Status</td>
                <td th:text="${student.address}">Address</td>
                <td>
                    <a href="@{/students/{id}(id=${student.stu_id})}">Edit</a>
                </td>
            </tr>
        </tbody>

【问题讨论】:

  • 定义:不工作(ing)?会发生什么?
  • 有什么问题?顺便说一句,我看到在 java 代码中,您在模型中添加了一个 student,而在模板代码中,您使用的是学生列表 students,所以它无法工作
  • 我有两个不同的页面,一个用于列出所有学生,一个用于编辑特定学生。我想从students.html 访问student.html。我编辑了我的问题。 @OlivierBoissé

标签: java spring spring-boot thymeleaf


【解决方案1】:

您在 &lt;a&gt; 标记内使用百里香表达式。您必须使用th:href 而不是href

    <td>
        <a th:href="@{/students/{id}(id=${student.stu_id})}">Edit</a>
    </td>

【讨论】:

  • 我怎么可能没有意识到...非常感谢!
  • @Phaki 它发生在我们最好的人身上。别担心。祝你好运。顺便说一句,如果我的回答真的解决了问题,请点赞并接受。
猜你喜欢
  • 2016-12-14
  • 2015-11-01
  • 2020-11-05
  • 1970-01-01
  • 2014-07-04
  • 2015-04-26
  • 2017-08-08
  • 2017-02-28
  • 2018-06-03
相关资源
最近更新 更多