Harsh Kanakhara
Harsh Kanakhara

Reputation: 89

RequestMapping not working properly

In my Spring Controller I have create 3 methods. Method 1 and Method 2 are working properly but Method 3 is giving me issue

Issue :

org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCEample1/students/edit/2] in DispatcherServlet with name 'SpringServlet'

Method 1 - Works Perfectly http://localhost:8080/SpringMVCEample1/students/get

@RequestMapping(value="/get", method = RequestMethod.GET)
public String getAllStudents(Model model){
    System.out.println("Fetching All Students");
    model.addAttribute("studentList", list);
    return "student";
}

Method 2 - Works Perfectly http://localhost:8080/SpringMVCEample1/students/1

@RequestMapping("/{id}")
public String getStudentById(@PathVariable("id") int id, Model model){
    System.out.println("Fetching Student with Id " + id);
    model.addAttribute("currentStudent",list.get(id));
    return "student";
}

Method 3 - Giving Error http://localhost:8080/SpringMVCEample1/students/edit/1

@RequestMapping(value="/edit/${studentId}")
    public String editStudent(@PathVariable("studentId") int studentId, Model model){
        System.out.println("Edit Student with Index " + studentId);
        model.addAttribute("studentId",studentId);
        model.addAttribute("studentName",list.get(studentId));
        return "redirect:get";
    }

Upvotes: 1

Views: 350

Answers (2)

Avinash
Avinash

Reputation: 4289

The mapping value should be {studentId} instead of ${studentId}.

@RequestMapping(value="/edit/{studentId}")
public String editStudent(@PathVariable("studentId") int studentId, Model model){
    System.out.println("Edit Student with Index " + studentId);
    model.addAttribute("studentId",studentId);
    model.addAttribute("studentName",list.get(studentId));
    return "redirect:get";
}

Upvotes: 0

boden
boden

Reputation: 1681

You must remove $ from @RequestMapping(value="/edit/${studentId}")

For example, it must be:

@RequestMapping(value="/edit/{studentId}")

Upvotes: 4

Related Questions