我有一个表单对象
public class TestForm {
private long id;
private List<Date> dates;
// getters and setters for the above
}
我的控制器有以下内容..
@RequestMapping(value = "/assignDummy", method = RequestMethod.POST)
public @ResponseBody
String assignDates(TestForm frm) {
System.out.println("frm:"+frm.getId()+", date:"+frm.getDates());
return "Everything is fine";
}
我的表格..
<form name="abc" method="post" action="assignDummy.htm">
<input type="text" name="id" value="1000">
<input type="text" name="dates[0]" value="4500000">
<input type="submit">
</form>
我收到以下错误..
无法将“java.lang.String”类型的属性值转换为 属性'dates [0]'的必需类型'java.util.Date';嵌套 例外是 org.springframework.core.convert.ConversionFailedException:失败 从类型java.lang.String转换为类型java.util.Date类型 '4500000';嵌套异常是java.lang.IllegalArgumentException“
感谢任何帮助。 提前致谢
答案 0 :(得分:2)
您正在尝试将字符串放入日期而不进行转换,因此它会崩溃。 您必须使用自定义属性编辑器才能将输入String转换为Date。
尝试添加您的控制器
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true);
binder.registerCustomEditor(Date.class, editor);
}