`
m635674608
  • 浏览: 4955999 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

SpringMVC 400 Bad Request 问题

    博客分类:
  • java
 
阅读更多

SpringMVC下,提交表单报400错:

Java代码   收藏代码
  1. description The request sent by the client was syntactically incorrect.  

 

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

 

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

Java代码   收藏代码
  1. @SuppressWarnings("deprecation")  
  2. @RequestMapping("/hello.do")  
  3. public String hello(HttpServletRequest request,HttpServletResponse response,  
  4.         @RequestParam(value="userName") String user  
  5. ){  
  6.     request.setAttribute("user", user);  
  7.     return "hello";  
  8. }  

 

这里,表单内必须提供一个userName的属性!
不想指定的话,你也可以定义这个属性的默认值defaultValue="":

Java代码   收藏代码
  1. @SuppressWarnings("deprecation")  
  2. @RequestMapping("/hello.do")  
  3. public String hello(HttpServletRequest request,HttpServletResponse response,  
  4.         @RequestParam(value="userName",defaultValue="佚名") String user  
  5. ){  
  6.     request.setAttribute("user", user);  
  7.     return "hello";  
  8. }  

 

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

Java代码   收藏代码
  1. @SuppressWarnings("deprecation")  
  2. @RequestMapping("/hello.do")  
  3. public String hello(HttpServletRequest request,HttpServletResponse response,  
  4.         @RequestParam(value="userName",required=false) String user  
  5. ){  
  6.     request.setAttribute("user", user);  
  7.     return "hello";  
  8. }  

 

2.上传问题

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

Java代码   收藏代码
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
  2.     <!-- 设置上传文件的最大尺寸1024字节=1K,这里是10K -->    
  3.     <property name="maxUploadSize">    
  4.         <value>10240</value>    
  5.     </property>  
  6.     <property name="defaultEncoding">    
  7.            <value>UTF-8</value>    
  8.     </property>    
  9. </bean>  

 

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

 

3.时间转换问题

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

Java代码   收藏代码
  1. The server encountered an internal error that prevented it from fulfilling this request  

 

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

Java代码   收藏代码
  1. @SuppressWarnings("deprecation")  
  2. @RequestMapping("/hello.do")  
  3. public String hello(HttpServletRequest request,HttpServletResponse response,  
  4.         @RequestParam(value="userName",defaultValue="佚名") String user,  
  5.         Date dateTest  
  6. ){  
  7.     request.setAttribute("user", user);  
  8.     System.out.println(dateTest.toLocaleString());  
  9.     return "hello";  
  10. }  

 

而网页上实际给的是

Java代码   收藏代码
  1. <input type="text" name="dateTest" value="2015-06-07">  

 

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

Java代码   收藏代码
  1. @InitBinder    
  2. public void initBinder(WebDataBinder binder) {    
  3.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
  4.     dateFormat.setLenient(false);    
  5.     binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, false));    
  6. }  

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

 

  1、提交表单数据类型与model不匹配
  2、方法参数顺序不正确

  3.Article的属性和你的form提交中的数据类型不 匹配

  4 对象类型与model类型不一致
  5 model类型不能为private,应为protected/public

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics