[英]Failed to convert string in java.util.Date with @DateTimeFormat(pattern=“dd/MM/yyyy”)
I have a simple POJO
with a Date field. 我有一个带有Date字段的简单
POJO
。 I want to bind the object with values from a form. 我想用一个表单中的值绑定对象。
In the form I'm using jquery ui datepicker
with date format("dd/mm/yyyy") 在表格中我使用日期格式的
jquery ui datepicker
(“dd / mm / yyyy”)
I have console.log
the value and is a string : 13-11-2014 我有
console.log
的值,是一个字符串:13-11-2014
I'm using spring 4.0.7
我正在使用
spring 4.0.7
I have on my dependencies joda-time 2.5 我有我的依赖joda-time 2.5
I get this exception: 我得到这个例外:
Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; 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 '22-11-2014'; nested exception is java.lang.IllegalArgumentException: Invalid format: "22-11-2014" is malformed at "-11-2014"]
My POJO: 我的POJO:
package gr.gsis.announcement.model;
import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class Announcement implements Serializable{
private static final long serialVersionUID = -1984554807132781312L;
private int id;
private String title;
private String bodyText;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date startDate;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date endDate;
private boolean activeFlag;
public Announcement() {
}
public Announcement(int id, String title, Date startDate, Date endDate,
boolean activeFlag) {
this.id = id;
this.title = title;
this.startDate = startDate;
this.endDate = endDate;
this.activeFlag = activeFlag;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBodyText() {
return bodyText;
}
public void setBodyText(String bodyText) {
this.bodyText = bodyText;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public boolean getActiveFlag() {
return activeFlag;
}
public void setActiveFlag(boolean activeFlag) {
this.activeFlag = activeFlag;
}
@Override
public String toString() {
return "Announcement [id=" + id + ", title=" + title + ", bodyText="
+ bodyText + ", startDate=" + startDate + ", endDate="
+ endDate + ", activeFlag=" + activeFlag + "]";
}
My controller: 我的控制器:
package gr.gsis.announcement.controller;
import gr.gsis.announcement.model.Announcement;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class AnnouncementController {
@RequestMapping("/")
public String welcome(Model model) {
model.addAttribute("greeting", "Welcome");
return "welcome";
}
@RequestMapping(value="/create", method = RequestMethod.GET)
public String getAnnouncementForm(Model model) {
Announcement announcement = new Announcement();
model.addAttribute("announcement", announcement);
return "announcementForm";
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processAnnouncement(@ModelAttribute("announcement") Announcement announcement) {
System.out.println(announcement);
return "announcementForm";
}
}
my Form JSP: 我的表单JSP:
<h2>Insert Announcement</h2>
<form:form method="post" commandName="announcement">
<div>
<form:label path="title">Title</form:label>
<form:input path="title" name="title" type="text"/>
</div>
<div>
<form:label path="startDate">Start Date</form:label>
<form:input path="startDate" cssClass="datepicker start" name="startDate" type="text"/>
</div>
<div>
<form:label path="endDate">End Date</form:label>
<form:input path="endDate" cssClass="datepicker end" name="endDate" type="text"/>
</div>
<div>
<form:label path="activeFlag">Active</form:label>
<form:checkbox path="activeFlag" name="activeFlag"/>
</div>
<div>
<input type="submit" class="button" value="Save" />
</div>
</form:form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });
});
What am I doing wrong? 我究竟做错了什么?
Thanks in advance 提前致谢
Your date format does not match input 您的日期格式与输入不匹配
@DateTimeFormat(pattern = "dd/MM/yyyy")
should be 应该
@DateTimeFormat(pattern = "dd-MM-yyyy")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.