不支持Spring MVC-Request方法'POST':org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported

时间:2014-01-21 12:34:29

标签: java spring spring-mvc

我收到以下警告: 请求方法“POST”不受支持。

控制器方法:

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

分配器一servlet.xml中

<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />

的index.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>

然而,如果我要更改方法= RequestMethod.GET并相应地在登录页面,那么它将起作用。

请注意,问题出在返回“static / profile.html”;

profile.html的FYI位置是WEB-INF / static /

谢谢!

2 个答案:

答案 0 :(得分:3)

当您将表单POST发送到HTTP服务器时,表单的内容不会作为查询参数发送;相反,表单(通常)上传为application/x-www-form-urlencoded类型的实体。不要在方法中使用@RequestParam,而是定义一个包含与表单字段对应的字段的Java类,并使用@ModelAttribute FormClass form

答案 1 :(得分:0)

你应该试试这个:

return "forward:/static/profile.html";