Spring MVC不支持请求方法"POST" [英] Request method 'POST' not supported in Spring mvc

查看:432
本文介绍了Spring MVC不支持请求方法"POST"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的表格:

<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错误,如下所示:

Now the issue is I am getting 405 error in console like below:

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

任何人都可以帮助我解决此错误吗?

Can any one please help me resolving this error ?

推荐答案

您首先需要使用GET方法显示表单,并需要使用POST提交表单.

You first need a GET method to display the form and a POST to submit it.

@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);
    ...
}

这篇关于Spring MVC不支持请求方法"POST"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆