RequestMapping无法正常工作

时间:2017-01-19 09:29:18

标签: java spring spring-mvc

在我的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";
    }

2 个答案:

答案 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";
}