zoukankan      html  css  js  c++  java
  • Spring MVC 学习之 400 错误

    如果一个方法中有参数被 @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() 
     */
    protectedModelAndView handleBindException(BindException ex, HttpServletRequest request, 
            HttpServletResponse response, Object handler)throws IOException { 
        response.sendError(HttpServletResponse.SC_BAD_REQUEST); 
        returnnew ModelAndView(); 
    }
  • 相关阅读:
    jvm 优化
    SqlServer体系结构
    sqlserver2012 在视图中建索引
    win10 桌面设置为远程桌面
    ORACLE 查询某表中的某个字段的类型,是否为空,是否有默认值等
    activemq读取剩余消息队列中消息的数量
    Oracl 一条sql语句 批量添加、修改数据
    ClickOnce一项Winform部署
    C#语言中的修饰符
    关于MySQL集群的一些看法
  • 原文地址:https://www.cnblogs.com/scwanglijun/p/3893980.html
Copyright © 2011-2022 走看看