我试图在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 class是:
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) {}
表单对象是,:object =" $ {persistDto}" ,百里香的输入是:
<input th:field="*{schedule.shChildTime}" type="text" class="form-control" id="inputShChildTime" th:placeholder="#{schedule.hour.real}"/>
更新
解决方案是在控制器上添加一个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));
}
示例数据格式:&#34; 2017-01-01 12:00:12&#34;
答案 0 :(得分:0)
可能是您的问题在下面的链接中提到: Unable to convert String to Date by requestBody in spring
或者你可以写转换器。你可以通过以下链接 http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/htmlsingle/#core-convert