-1

I know theres a similar question in stackoverflow like mine. I tried every answer that i could from this question none of these worked: Thymeleaf using path variables to th:href

Can you help me what did i wrong in my thymeleaf page? 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);
    } 

Href 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>
Phaki
  • 210
  • 4
  • 18
  • 1
    define: is not work(ing)? what happens? – Stultuske Mar 27 '21 at 17:40
  • what is the problem ? By the way, I see thatin the java code you are adding a single `student` into the model, whereas in the template code you are using a list of student `students` so it can't work – Olivier Boissé Mar 27 '21 at 17:46
  • I have two different pages, one for list all the students and one for edit a specific student. I would like to reach the student.html from students.html. I edited my question. @OlivierBoissé – Phaki Mar 27 '21 at 17:55

1 Answers1

1

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>
omar jayed
  • 850
  • 5
  • 16