为什么不支持 SpringMVC 请求方法“GET”?

新手上路,请多包涵

我在尝试 @RequestMapping(value = "/test", method = RequestMethod.POST) 但错误

代码是

 @Controller
 public class HelloWordController {
 private Logger logger = LoggerFactory.getLogger(HelloWordController.class);

 @RequestMapping(value = "/test", method = RequestMethod.POST)
 public String welcome() {
  logger.info("Spring params is welcome");
  return "/WEB-INF/jsp/welcome";
 }

}

web.xml 是

<servlet>
<description>This is Spring MVC DispatcherServlet</description>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <description>SpringContext</description>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

   <servlet-mapping>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>

springmvc.xml 是

index.jsp是

<form action="<%=request.getContextPath() %>/test" method="post">
<input type="submit" value="submit">
</form>

我输入提交按钮浏览器是错误的

HTTP 状态 405 - 请求方法“GET”不支持类型状态报告

不支持消息请求方法“GET”

说明 请求的资源不允许使用指定的 HTTP 方法(不支持请求方法“GET”)。

原文由 EdwardLau 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 748
2 个回答

改变

@RequestMapping(value = "/test", method = RequestMethod.POST)

@RequestMapping(value = "/test", method = RequestMethod.GET)

原文由 Bik 发布,翻译遵循 CC BY-SA 3.0 许可协议

method = POST 如果您将表单“发布”到 url /test,将会起作用。

如果您在浏览器的地址栏中键入一个 url 并按回车键,它始终是一个 GET 请求,因此您必须指定 POST 请求。

谷歌 HTTP GETHTTP POST (还有其他几个,如 PUT DELETE)。他们都有自己的意义。

原文由 Tejas 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题