[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]springMvc提交表单,报错:Cannot convert value of type [java.lang.String] to required type [java.util.Dat

上一篇:[转载]解决android sdk 无法更新
下一篇:[备忘]jquery validate小提醒

添加日期:2015/10/20 9:19:22 快速返回   返回列表 阅读23350次
原文:
http://www.cnblogs.com/rollenholt/p/3890415.html
-------------------------------------------
bean里类型是Date,页面输入2015-10-01,无法转换为Date类型。

相应的解决办法为:

在对应的controller中增加属性编辑器:


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


注意这块的new CustomDateEditor(dateFormat, true)中的true,查看CustomDateEditor源码可以看到:


/**
 * Create a new CustomDateEditor instance, using the given DateFormat
 * for parsing and rendering.
 * <p>The "allowEmpty" parameter states if an empty String should
 * be allowed for parsing, i.e. get interpreted as null value.
 * Otherwise, an IllegalArgumentException gets thrown in that case.
 * @param dateFormat DateFormat to use for parsing and rendering
 * @param allowEmpty if empty strings should be allowed
 */
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {
    this.dateFormat = dateFormat;
    this.allowEmpty = allowEmpty;
    this.exactDateLength = -1;
}


当allowEmpty字段为true的时候form表单传递的值可以为空。
否则会出现""字符串解析为date报错。
 

评论 COMMENTS
guest522291499
2018/7/4 18:06:05
Cannot convert value of type 'java.lang.String' to required type 'java.util.Date': no matching edito

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved