gpt4 book ai didi

java - 为什么不支持 SpringMVC 请求方法 'GET'?

转载 作者:IT老高 更新时间:2023-10-28 13:50:01 25 4
gpt4 key购买 nike

我尝试 @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 Status 405 - Request method 'GET' not supported type Status report

message Request method 'GET' not supported

description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).

最佳答案

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

如果你在浏览器的地址栏中输入一个url然后回车,它总是一个GET请求,所以你必须指定POST请求。

Google for HTTP GETHTTP POST(还有其他几个,例如 PUT DELETE)。它们都有各自的含义。

关于java - 为什么不支持 SpringMVC 请求方法 'GET'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3333611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com