请求方法' POST' Spring mvc不支持

时间:2015-03-25 09:36:16

标签: spring-mvc httprequest httpresponse http-response-codes

这是我的表格:

<form action="${pageContext.request.contextPath}/admin/editc" method="POST" id="editForm">
    <input type="text" name="username" class="form-control" />
    <input type="text" name="password" class="form-control" />
    <input type="submit" value="submit" >
</form>

这是我的控制器方法:

@RequestMapping(value = "/admin/edit", method = RequestMethod.GET)
public ModelAndView editPage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "User edit Form - Database Interaction");
    model.addObject("message", "This page is for ROLE_ADMIN only!");
    model.setViewName("editpage");
    System.out.println("getting edit page");

    return model;

}


@RequestMapping(value = "/admin/editc", method = RequestMethod.POST)
public ModelAndView updateCredentials() {
//      System.out.println("Username= "+username+"  password= "+password);
     ModelAndView model = new ModelAndView();
    model.addObject("title", "Credential Edit Operation");
    model.addObject("message", "You are successfully updated your credentials");
    model.addObject("edited", "TRUE");
    model.setViewName("editpage");
    System.out.println("executed updateCredentials POST method");

    return model;

}

现在问题是我在控制台中遇到405错误,如下所示:

org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
 WARNING: Request method 'POST' not supported

任何人都可以帮我解决这个错误吗?

2 个答案:

答案 0 :(得分:0)

您的java方法的签名是错误的,您还需要添加您期望从POST中获得的内容。请查看此guide以更正您的代码。

答案 1 :(得分:0)

首先需要一个GET方法来显示表单,然后需要一个POST来提交它。

@RequestMapping(value = "/admin/editc", method = RequestMethod.GET)
public ModelAndView updateCredentials() {
    return new ModelAndView("editPage");
}

@RequestMapping(value = "/admin/editc", method = RequestMethod.POST)
public ModelAndView updateCredentials(@ModelAttribute UserCredentials credentials) {
    someService.save(credentials);
    ...
}