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

2022-09-01 11:48:32

我已尝试,但存在错误@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";
 }

}

网络.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 是

索引.jsp是

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

我输入提交 botton 浏览器错误

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

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

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


答案 1

method = POST如果您将表单“发布”到网址/test,则可以使用。

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

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


答案 2

改变

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

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

推荐