问题描述:使用My97DatePicker插件将前台时间数据传递给后台出现字符串转Date格式问题。
以下为报错信息:
Field error in object 'TWeekConfig' on field 'deptLineTime': rejected value [05:05]; codes [typeMismatch.TWeekConfig.deptLineTime,typeMismatch.deptLineTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [TWeekConfig.deptLineTime,deptLineTime]; arguments []; default message [deptLineTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'deptLineTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '05:05'; nested exception is java.lang.IllegalArgumentException: Unable to parse '05:05']
Field error in object 'TWeekConfig' on field 'userLineTime': rejected value [7:00]; codes [typeMismatch.TWeekConfig.userLineTime,typeMismatch.userLineTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [TWeekConfig.userLineTime,userLineTime]; arguments []; default message [userLineTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'userLineTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '7:00'; nested exception is java.lang.IllegalArgumentException: Unable to parse '7:00']
主要问题就是数据格式错误,这个bug问题花了好长时间,最后解决却发现非常简单,使用注解便可解决,将注解中时间格式更改即可。
先将有Bug的代码放上:
界面代码:
<div class="form-group">
<label class="control-label col-md-1">部门提交到公司汇总截止时间点</label>
<div class="col-md-3">
<div class="input-icon right">
<input type="text" id="deptLineTime" onclick="WdatePicker({skin:'whyGreen',dateFmt:'H:mm:ss'})" class="form-control" autocomplete="off" name="deptLineTime" placeholder="请输入时间点(不可早于个人提交时间点)">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-1">个人提交到公司汇总截止时间点</label>
<div class="col-md-3">
<div class="input-icon right">
<input type="text" id="userLineTime" class="form-control" onclick="WdatePicker({skin:'whyGreen',dateFmt:'H:mm:ss'})" autocomplete="off" name="userLineTime" placeholder="请输入时间点(不可晚于部门提交时间点)">
</div>
</div>
</div>
注意,前端收到的时间格式为时间点(HH:MM:SS),所以与其他的格式不同,这点一开始没有注意到,才耽误了好多时间。
接下来是对应的实体类。可见注解(SpringMVC)中显示的是年月格式。
/**提交至部门截止天数*/
private Integer userLineDay;
/**提交至公司截止时间*/
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date userLineTime;
/**提交至公司截止天数*/
private Integer deptLineDay;
/**提交至部门截止时间*/
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date deptLineTime;
应该更改注解格式为时间点就好,修改后的代码。
/**提交至部门截止天数*/
private Integer userLineDay;
/**提交至公司截止时间*/
@DateTimeFormat(pattern = "HH:mm:ss")
private Date userLineTime;
/**提交至公司截止天数*/
private Integer deptLineDay;
/**提交至部门截止时间*/
@DateTimeFormat(pattern = "HH:mm:ss")
private Date deptLineTime;
这次问题解决。
感谢在解决这次问题中索搜到的这及篇内容不错的博客:
SpringMVC 处理Date类型@DateTimeFormat @InitBinder
springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
一起学习,共同成长。