Java Spring无法识别DateTime字段的格式

Java Spring无法识别DateTime字段的格式,java,spring,jsp,datetime,spring-mvc,Java,Spring,Jsp,Datetime,Spring Mvc,我正在使用Spring3.2.4开发web应用程序。我有一些包含日期和时间字段的表单。我的jsp的一部分: <form:form method="post" action="/add" modelAttribute="licence"> ... <form:input type="datetime" path="beginDate"/> <form:input type="datetime" path="endDate"/> &

我正在使用Spring3.2.4开发web应用程序。我有一些包含日期和时间字段的表单。我的jsp的一部分:

<form:form method="post" action="/add" modelAttribute="licence">
    ...
    <form:input type="datetime" path="beginDate"/>
    <form:input type="datetime" path="endDate"/>
    <form:input path="quantityLimit"/>
    ...
</form:form>
此外,我还向servlet配置xml中添加了
,如一些博客所述

有一个目标控制器:

@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    dateFormat.setLenient(true);
    webDataBinder.registerCustomEditor(DateTime.class, new CustomDateEditor(dateFormat, true));
}
@RequestMapping(value = "/{softwareId}/licence/add", method = RequestMethod.POST)
public String addLicence(@PathVariable("softwareId") Long softwareId, Licence licence, Model model) {
    Software software = softwareRepository.findOne(softwareId);
    licence.setSoftware(software);
    licenceRepository.save(licence);
    return ADMIN_PATH + "softwareEdit";
}
软件类如下所示:

@Entity
@Table(name = "licences")
public class Licence {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "begin_date")
private DateTime beginDate;

@Column(name = "end_date")
private DateTime endDate;

@Column(name = "quantity_limit")
private Long quantityLimit;

@ManyToOne
private Software software;

//getters, setters, etc.
}
问题是:当我在dateTime字段为空的情况下提交表单时,它工作得非常好,但是当我在date字段中有任何内容时(无论其格式是否正确),我会收到
HTTP错误400:Bad请求
。控制台中没有异常,只有错误的请求,但我非常确定它与日期解析有关


在Spring应用程序中,是否有一种处理表单中日期和时间字段的详细描述方法?

简化您的工作,使用
@DateTimeFormat
,摆脱
WebDataBinder
配置。似乎
CustomDateEditor
只适用于
java.util.Date
,Spring没有其他(默认/未指定)机制将
字符串转换为
DateTime

这是一种机制

@Column(name = "begin_date")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private DateTime beginDate;

将日志记录设置为调试。春天会告诉你发生了什么。另外,向我们展示您的命令类。使用firebug并查看“Net”选项卡和传出的参数,您可能会得到一些线索。因此,当表单发送时,在调试级别上的日志记录实际上什么都没有显示。此外,Chrome、Firefox和Safari中的net选项卡显示所有字段的正常帖子。这是不可能的。检查你的伐木工人。Spring在响应400时总是记录一些内容。