Java 不支持 Spring MVC-Request 方法“POST”:org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21258294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC-Request method 'POST' not supported: org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
提问by Touchstone
I am getting the following warning: Request method 'POST' not supported.
我收到以下警告:不支持请求方法“POST”。
Controller method:
控制器方法:
@Controller
public class UserServiceController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public String login(@RequestParam Map<String,String> requestParams) throws Exception{
System.out.println(requestParams.values());
loginService.userAuthentication(requestParams.get("loginuname"), requestParams.get("loginpassword"));
System.out.println("Before return");
return "static/profile.html";
}
}
Dispatcher-servlet.xml
Dispatcher-servlet.xml
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
index.html
索引.html
<script language="javascript" type="text/javascript">
function submitForm(){
document.login.submit();
}
</script>
<div id="login" class="login">
<form action="http://localhost:8080/SampleApp/login" name="login" method="post">
<input type="text" value = "Email" name="loginuname" id="loginuname" class="loginbasetxt">
<input type="text" value="Password" name="loginpassword" id="loginpassword" class="loginbasetxt">
<img src="static/img/tb-login-button.png" onclick="submitForm()"/>
</form>
</div>
However, if i would change the method=RequestMethod.GET and correspondingly at login page then it would work.
但是,如果我要更改 method=RequestMethod.GET 并相应地在登录页面上更改,那么它将起作用。
Please note, problem is at return "static/profile.html";
请注意,问题在于返回“static/profile.html”;
FYI location of profile.html is WEB-INF/static/
profile.html 的 FYI 位置是 WEB-INF/static/
Thanks!!
谢谢!!
回答by chrylis -cautiouslyoptimistic-
When you POST
a form to an HTTP server, the form's contents aren't sent as query parameters; instead, the form is (usually) uploaded as an entity of type application/x-www-form-urlencoded
. Instead of using @RequestParam
in your method, define a Java class that has fields corresponding to the form fields, and use @ModelAttribute FormClass form
.
当您POST
将表单发送到 HTTP 服务器时,表单的内容不会作为查询参数发送;相反,表单(通常)作为类型的实体上传application/x-www-form-urlencoded
。不要@RequestParam
在您的方法中使用,而是定义一个具有与表单字段对应的字段的 Java 类,并使用@ModelAttribute FormClass form
.
回答by uzor
you should try this:
你应该试试这个:
return "forward:/static/profile.html";