gpt4 book ai didi

java - RequestMapping 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 08:38:55 27 4
gpt4 key购买 nike

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

最佳答案

您必须从 @RequestMapping(value="/edit/${studentId}") 中移除 $

例如,它必须是:

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

关于java - RequestMapping 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com