当我部署我的代码时,我收到错误
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'programController' bean method
public java.lang.String com.quizapp.beans.controller.panal.ProgramController.editProgram(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'programController' bean method
public java.lang.String com.quizapp.beans.controller.panal.ProgramController.setupProgram(javax.servlet.http.HttpSession,org.springframework.ui.Model) mapped.
下面是我的programController类
@Controller
public class ProgramController {
@RequestMapping(name = "/editProgram", method = RequestMethod.GET)
public String editProgram(HttpServletRequest request, HttpServletResponse response) {
if (request.getSession().getAttribute(Constants.LOGGED_IN_USER) != null) {
ProgramEntity program = new ProgramEntity();
if (request.getParameter("id") == null) {// create
program.setType("create");
} else {// edit
program.setType("edit");
program.setCode(request.getParameter("id"));
}
request.setAttribute("programBean", program);
return "editProgram";
} else {
return "redirect:/login";
}
}
@RequestMapping(name = "/program", method = RequestMethod.GET)
public String setupProgram(HttpSession session, Model model) {
if (session.getAttribute(Constants.LOGGED_IN_USER) != null) {
model.addAttribute("programBean", new ProgramEntity());
// if (request.getSession().getAttribute(Constants.LOGGED_IN_USER) != null) {
// request.setAttribute("programBean", new ProgramEntity());
return "viewProgram";
} else {
return "redirect:/login";
}
}
}
当我将方法editProgram设为POST时,它会完美地部署。 为什么添加两个GET方法有问题。 StackOverflow上有许多类似的问题,但我找不到正确的答案。
答案 0 :(得分:4)
因为您指定RequestMapping
value
而不是names
。目前,两种方法的""
都是相同的(value
- 空),这是不允许的。您的请求映射的网址部分应在name
中指定,而不是values
的CRLF
属性。
答案 1 :(得分:1)
试试这个:
@Controller
public class ProgramController {
@RequestMapping(value = "/editProgram", method = RequestMethod.GET)
public String editProgram(HttpServletRequest request, HttpServletResponse response) {
if (request.getSession().getAttribute(Constants.LOGGED_IN_USER) != null) {
ProgramEntity program = new ProgramEntity();
if (request.getParameter("id") == null) {// create
program.setType("create");
} else {// edit
program.setType("edit");
program.setCode(request.getParameter("id"));
}
request.setAttribute("programBean", program);
return "editProgram";
} else {
return "redirect:/login";
}
}
@RequestMapping(value= "/program", method = RequestMethod.GET)
public String setupProgram(HttpSession session, Model model) {
if (session.getAttribute(Constants.LOGGED_IN_USER) != null) {
model.addAttribute("programBean", new ProgramEntity());
// if (request.getSession().getAttribute(Constants.LOGGED_IN_USER) != null) {
// request.setAttribute("programBean", new ProgramEntity());
return "viewProgram";
} else {
return "redirect:/login";
}
}
}