在我的Spring Controller中,我创建了3种方法。方法1和方法2正常工作,但方法3给我提问
问题:
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCEample1/students/edit/2] in DispatcherServlet with name 'SpringServlet'
方法1 - 完美运作 的 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";
}
方法2 - 完美运作 的 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";
}
方法3 - 给出错误 的 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";
}
答案 0 :(得分:3)
您必须从$
@RequestMapping(value="/edit/${studentId}")
例如,它必须是:
@RequestMapping(value="/edit/{studentId}")
答案 1 :(得分:0)
映射值应为{studentId}而不是$ {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";
}