Spring MVC 学习之 400 错误(1)

如果一个方法中有参数被 @Valid 标注了,但该参数后面没有紧跟一个 BindingResult 类型的参数,那么提交到该方法时,将返回 400 错误。

错误消息将会是:The request sent by the client was syntactically incorrect ().

错误的方法定义 1 :(错误原因:没有 BindingResult 参数

@RequestMapping ({ "error400" }) 
public String error400( @Valid @ModelAttribute ( "testForm" ) TestForm testForm) { 
     return "test"
}

错误的方法定义 2 :(错误原因:没有紧跟在 @Valid 参数之后

@RequestMapping ({ "error400" })
public String error400( @Valid @ModelAttribute ( "testForm" ) TestForm testForm, Model model, Errors result) {
     return "demo/front/test" ;
}

也可参考 DefaultHandlerExceptionResolver.java 中的方法说明:

/** 
  * Handle the case where an {@linkplain ModelAttribute @ModelAttribute} method 
  * argument has binding or validation errors and is not followed by another 
  * method argument of type {@link BindingResult}. 
  * By default an HTTP 400 error is sent back to the client. 
  * @param request current HTTP request 
  * @param response current HTTP response 
  * @param handler the executed handler 
  * @return an empty ModelAndView indicating the exception was handled 
  * @throws IOException potentially thrown from response.sendError() 
  */
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request, 
         HttpServletResponse response, Object handler) throws IOException { 
     response.sendError(HttpServletResponse.SC_BAD_REQUEST); 
     return new ModelAndView(); 
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值