为什么不支持SpringMVC Request方法'GET'?

Edw*_*Lau 23 java spring spring-mvc

我尝试@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";
 }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

  <servlet-mapping>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
Run Code Online (Sandbox Code Playgroud)

和springmvc.xml是

index.jsp是

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

我输入提交botton brower是错误

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

消息请求方法'GET'不受支持

description对于请求的资源,不允许使用指定的HTTP方法(不支持请求方法'GET').

Tej*_*jas 20

method = POST 如果您将表单"发布"到网址/测试,则会有效.

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

谷歌HTTP GETHTTP POST(还有其他几个像PUT DELETE).他们都有自己的意思.

  • 这并不能解决问题,只是解释了基础知识。通过 POST 发送表单时,始终出现“已解决 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法 'GET']”。 (3认同)

小智 13

更改

@RequestMapping(value = "/test", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)

@RequestMapping(value = "/test", method = RequestMethod.GET)
Run Code Online (Sandbox Code Playgroud)

  • 为什么`method = RequestMethod.POST`不起作用?表单方法是POST,操作URL是/ test,所以我认为它会起作用. (10认同)