org.springframework.validation.BindException问题解决

本文介绍了在使用SpringMVC框架进行日期字段绑定时遇到的问题及解决方法。具体表现为前端传递的日期字符串无法正确转换为Java.util.Date类型,导致类型不匹配错误。通过检查和修正代码中的日期类型引用,成功解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'hiredate': rejected value [20150511]; codes [typeMismatch.user.hiredate,typeMismatch.hiredate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.hiredate,hiredate]; arguments []; default message [hiredate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'hiredate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'hiredate': no matching editors or conversion strategy found]
今天项目中用到了springmvc,前台有个输入框hiredate,传到后台User模型中的 (Date) hiredate中,并且写@InitBinder的自定义转换器
// 自定义类型转换器 @InitBinder public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { System.out.println("initBinder &&&&"+request.getParameter("hiredate")+"***"+request.getParameter("username")); binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyyMMdd"), true)); }

可是还是一直报错。各种调查后发现Data.class的类竟然导入的是Java.sql.Data包中的。 后修正为java.util.Data.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值