zoukankan      html  css  js  c++  java
  • SpringMVC 400 Bad Request 问题

    转自:http://m635674608.iteye.com/blog/2231970
    作者:m635674608

    SpringMVC 下,提交表单报400错:

    description The request sent by the client was syntactically incorrect.  

    根据网上的总结,可能是因为如下几个问题引起的

    参数指定问题

    如果Controller中定义了参数,而表单内却没有定义该字段

    @SuppressWarnings("deprecation")  
    @RequestMapping("/hello.do")  
    public String hello(HttpServletRequest request,HttpServletResponse response,
            @RequestParam(value="userName") String user){  
        request.setAttribute("user", user);  
        return "hello";  
    }  

    这里,表单内必须提供一个userName的属性!

    不想指定的话,你也可以定义这个属性的默认值 defaultValue=”“

    @SuppressWarnings("deprecation")  
    @RequestMapping("/hello.do")  
    public String hello(HttpServletRequest request,HttpServletResponse response,  
            @RequestParam(value="userName",defaultValue="佚名") String user  ){  
        request.setAttribute("user", user);  
        return "hello";  
    }  

    也可以指定该参数是非必须的required=false

    @SuppressWarnings("deprecation")  
    @RequestMapping("/hello.do")  
    public String hello(HttpServletRequest request,HttpServletResponse response,  
            @RequestParam(value="userName",required=false) String user ){  
        request.setAttribute("user", user);  
        return "hello";  
    }  

    上传问题

    上传文件大小超出了Spring上传的限制

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
        <!-- 设置上传文件的最大尺寸1024字节=1K,这里是10K -->    
        <property name="maxUploadSize">    
            <value>10240</value>    
        </property>  
        <property name="defaultEncoding">    
               <value>UTF-8</value>    
        </property>    
    </bean>  

    我们工程里面是这个问题引起的,但是我实际示例中发现超过大小是直接报错的。

    时间转换问题

    也有网友说是因为时间转换引起的,而我实际操作中发现报错是:

    The server encountered an internal error that prevented it from fulfilling this request  

    这里也顺便提一下,假如你的Controller要一个时间对象,代码如下:

    @SuppressWarnings("deprecation")  
    @RequestMapping("/hello.do")  
    public String hello(HttpServletRequest request,HttpServletResponse response,  
            @RequestParam(value="userName",defaultValue="佚名") String user,  
            Date dateTest  ){  
        request.setAttribute("user", user);  
        System.out.println(dateTest.toLocaleString());  
        return "hello";  
    }  

    而网页上实际给的是

    <input type="text" name="dateTest" value="2015-06-07">  

    这里需要在Controller增加一个转换器

    @InitBinder    
    public void initBinder(WebDataBinder binder) {    
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
        dateFormat.setLenient(false);    
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));    
    }  

    http://cuisuqiang.iteye.com/blog/2054234

    1. 提交表单数据类型与model不匹配
    2. 方法参数顺序不正确
    3. Article的属性和你的form提交中的数据类型不 匹配
    4. 对象类型与model类型不一致
    5. model类型不能为private,应为protected/public
  • 相关阅读:
    pxeconfig 4.2.0 发布,PXE 首要启动设备
    Nutz 1.b.48 发布,Java 应用框架
    Exponent CMS 2.2.0 Beta3 发布
    持续交付加速了创新的步伐
    Fork CMS 3.5.1 发布,PHP 的 CMS 系统
    Org Mode 8.0 发布,TODO 管理工具
    Apache OpenNLP 1.5.3 发布
    结巴分词 0.27 发布,Python 中文分词组件
    Linux 内核 3.9 开发统计
    红帽发布开源的 OpenStack RDO
  • 原文地址:https://www.cnblogs.com/Sherlock-J/p/12925973.html
Copyright © 2011-2022 走看看