javaspringspring-bootthymeleaf

Spring boot thymeleaf mapping is not working for me


I know there's a similar question on StackOverflow like mine. I've tried every answer that I could find from this question but none of these worked: Thymeleaf using path variables to th:href

Can you help me to fix my thymeleaf page? The URL that I would like to reach: localhost:8081/students/{stu_id}

My GetMapping looks like:

@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);
} 

HTML block that I use (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>

Solution

  • You are using thymeleaf expression inside <a> tag. You have to use th:href instead of href.

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