我有一个带有日期字段的简单POJO
。我想用一个表单中的值绑定对象
在表格中我使用jquery ui datepicker
日期格式(“dd / mm / yyyy”)
我有console.log
的值,是一个字符串:13-11-2014
我正在使用spring 4.0.7
我有我的依赖joda-time 2.5
我得到了这个例外:
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"]
我的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 + "]";
}
我的控制器:
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";
}
}
我的表单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' });
});
我做错了什么?
提前致谢
答案 0 :(得分:19)
您的日期格式与输入
不匹配@DateTimeFormat(pattern = "dd/MM/yyyy")
应该是
@DateTimeFormat(pattern = "dd-MM-yyyy")