现象
利用 SpringMVC 后端框架,在页面利用表单 (method="post") 提交数据,结果页面报 405 错误,提示 "Request method 'POST' not supported"。
前提
若以下前提和你的项目不匹配,那解决方案可能不适用于你的项目中哦!
- 全部前端页面采用 html 文件
- Spring 4.3.13 版本(低于请更新。啰嗦一句,团队更新框架不仅置入新功能,而且会修复旧版本的bug,Spring框架的mvn仓库网址)
- Controller 类用到 @Controller 和 @RequestMapping 注解
实验
- 为了方便理解,ViewResolver的后缀先设为空:
- /resource/html/文件夹内容:
- Controller 类代码:
- first.html 页面的表单代码
步骤重现
- 往地址栏中输入 http://……(省略)/myProfile/setting,在表单处输入数据。
- 数据会被发送至 registerUser 方法并处理。
(数据被成功传输至后台,证明 System.out.println(user); 之前并无出错)
3. 无法跳转至 first.html,并报 405 错误。
分析
由于使用的是 html 文件,它并不支持响应头带有 post 的应答包,所以会报错。
解决方案
- 若条件允许,使用 jsp 等能够接收 post 应答包的页面文件。(快捷,但不建议,jsp 会增加前后端耦合)
- 使用 “redirect:映射路径”进行跳转:
以上两种方案皆可完成跳转,并正常运行。