模糊映射错误:Spring Controller

时间:2016-03-31 06:36:45

标签: java spring spring-mvc controller mapping

当我部署我的代码时,我收到错误

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上有许多类似的问题,但我找不到正确的答案。

2 个答案:

答案 0 :(得分:4)

因为您指定RequestMapping value而不是names。目前,两种方法的""都是相同的(value - 空),这是不允许的。您的请求映射的网址部分应在name中指定,而不是valuesCRLF属性。

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