SpringMVC下,提交表单报400错:
Java代码
description The request sent by the client was syntactically incorrect.
根据网上的总结,可能是因为如下几个问题引起的
1.参数指定问题
如果Controller中定义了参数,而表单内却没有定义该字段
Java代码
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
publicString hello(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="userName") String user
){
request.setAttribute("user", user);
return"hello";
}
这里,表单内必须提供一个userName的属性!
不想指定的话,你也可以定义这个属性的默认值defaultValue="":
Java代码
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
publicString hello(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="userName",defaultValue="佚名") String user
){
request.setAttribute("user", user);
return"hello";
}
也可以指定该参数是非必须的required=false:
Java代码
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
publicString hello(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="userName",required=false) String user
){
request.setAttribute("user", user);
return"hello";
}
2.上传问题
上传文件大小超出了Spring上传的限制
Java代码
10240
UTF-8
我们工程里面是这个问题引起的,但是我实际示例中发现超过大小是直接报错的。
3.时间转换问题
也有网友说是因为时间转换引起的,而我实际操作中发现报错是:
Java代码
The server encountered an internal error that prevented it from fulfillingthisrequest
这里也顺便提一下,假如你的Controller要一个时间对象,代码如下:
Java代码
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
publicString hello(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="userName",defaultValue="佚名") String user,
Date dateTest
){
request.setAttribute("user", user);
System.out.println(dateTest.toLocaleString());
return"hello";
}
而网页上实际给的是
Java代码
这里需要在Controller增加一个转换器
Java代码
@InitBinder
publicvoidinitBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,newCustomDateEditor(dateFormat,false));
}
1、提交表单数据类型与model不匹配
2、方法参数顺序不正确
3.Article的属性和你的form提交中的数据类型不匹配
4对象类型与model类型不一致
5 model类型不能为private,应为protected/public