繁体   English   中英

使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date]

[英]Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”)

我试图验证在Spring 4.3上使用JQuery Timepicker生成的@DateTimeFormat(pattern =“ HH:mm”),并且在将数据对象发送到控制器时出现问题,这会生成异常。

Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property schedule.shChildTime; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "00:00"

数据库的属性为:

sh_child_time time without time zone NOT NULL DEFAULT '00:00:00'::time without time zone

Hibernate逆向工程进行类映射,并添加符号,Schedule类的属性为:

private boolean shHappen;
private char shStatus;
private short shOrder;
@DateTimeFormat(pattern = "HH:mm")
private Date shChildTime;
private int[][] shOrderMatrix;

控制器正在通过ModelAttribute接收Schedule对象:

@RequestMapping(value = "/schedules/new", method = RequestMethod.POST)
public String saveSchedule(@ModelAttribute("persistDto")Schedule schedule, BindingResult result,Model model,Locale locale,HttpServletRequest request) {}

表单对象是th:object =“ $ {persistDto}” ,百里香的输入是:

<input th:field="*{schedule.shChildTime}" type="text" class="form-control" id="inputShChildTime" th:placeholder="#{schedule.hour.real}"/>

UPDATE

解决方案是在控制器上添加一个InitBinder,该数据具有我在模型上发送的数据日期格式,并在前端字段中格式化数据:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

数据格式示例:“ 2017-01-01 12:00:12”

可能是您的问题与以下链接中提到的问题相同: 无法在春季通过requestBody将String转换为Date

或者您可以编写转换器。 您可以通过下面的链接http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/htmlsingle/#core-convert

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM