SpringMVC:尝试 POST 重定向请求调用时抛出 415 不受支持的媒体类型
SpringMVC : Throws 415 Unsupported Media Type while attempting POST request call for redirection
浏览器响应如下:
服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
而不是重定向到主页(home.jsp)
welcome.jsp 文件:
<form method ="POST" action = "<c:url value='/login'/>" >
<input id="name" name="name" type="text">
<input id="password" name="password" type="password">
<input type="submit" id="loginNew" value="LoginNew">
</form>
控制器class:
@RequestMapping( value="/login" , method = RequestMethod.POST )
public ModelAndView authenticate ( @RequestBody User userObj ) throws Exception {
ModelAndView modelAndView = new ModelAndView ();
try {
user = userService.authenticateUser ( userObj );
} catch ( Exception e ) {
e.printStackTrace();
}
modelAndView.setViewName("/home");
return modelAndView;
}
通过在方法参数中添加 @ModelAttribute 而不是 @RequestBody 解决了问题,然后按预期重定向到 home.jsp 页面。
@RequestMapping( value="/login" , method = RequestMethod.POST )
public ModelAndView authenticate ( @ModelAttribute User userObj ) throws Exception {
ModelAndView modelAndView = new ModelAndView ();
try {
user = userService.authenticateUser ( userObj );
} catch ( Exception e ) {
e.printStackTrace();
}
modelAndView.setViewName("/home");
return modelAndView;
}
浏览器响应如下:
服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
而不是重定向到主页(home.jsp)
welcome.jsp 文件:
<form method ="POST" action = "<c:url value='/login'/>" >
<input id="name" name="name" type="text">
<input id="password" name="password" type="password">
<input type="submit" id="loginNew" value="LoginNew">
</form>
控制器class:
@RequestMapping( value="/login" , method = RequestMethod.POST )
public ModelAndView authenticate ( @RequestBody User userObj ) throws Exception {
ModelAndView modelAndView = new ModelAndView ();
try {
user = userService.authenticateUser ( userObj );
} catch ( Exception e ) {
e.printStackTrace();
}
modelAndView.setViewName("/home");
return modelAndView;
}
通过在方法参数中添加 @ModelAttribute 而不是 @RequestBody 解决了问题,然后按预期重定向到 home.jsp 页面。
@RequestMapping( value="/login" , method = RequestMethod.POST )
public ModelAndView authenticate ( @ModelAttribute User userObj ) throws Exception {
ModelAndView modelAndView = new ModelAndView ();
try {
user = userService.authenticateUser ( userObj );
} catch ( Exception e ) {
e.printStackTrace();
}
modelAndView.setViewName("/home");
return modelAndView;
}