简体   繁体   English

如何使用@DateTimeFormat以spring形式绑定日期字段?

[英]How to bind date field in spring form using @DateTimeFormat?

I am looking for solution with my current problem. 我正在寻找解决当前问题的解决方案。 I have a classic POJO mapped by Hibernate with Date variable. 我有一个由Hibernate映射的经典POJO和Date变量。

@Entity
@Table(name="record")
public class Record implements Serializable {

    @Column(name = Record.COLUMN_CREATED)
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd.MM.yyyy hh:mm")
    private Date created;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}

This entity is saved in MySQL database in TIMESTAMP variable (2013-09-25 22:13:18.000). 该实体在TIMESTAMP变量(2013-09-25 22:13:18.000)中保存在MySQL数据库中。

I am using Spring form to show data saved in my POJO. 我使用Spring表单显示保存在我的POJO中的数据。

<form:form method="post" commandName="record" action="record/update.htm">
  <table>
    <tr>
      <td><form:label path="<%=Record.COLUMN_CREATED%>"><spring:message code="administration.record.created"/>:</form:label></td>
      <td><form:input path="<%=Record.COLUMN_CREATED%>"/></td>
    </tr>
  </table>  
</form:form>

My problem is, when I want to edit this POJO and I send it into spring form to show, I get Date as 2013-09-25 22:13:18.000, exactly in the same format as MySQL timestamp. 我的问题是,当我想编辑这个POJO并将其发送到spring形式显示时,我得到Date为2013-09-25 22:13:18.000,与MySQL时间戳的格式完全相同。 But I want to get this Date formated according the pattern, which I set using @DateTimeFormat annotation. 但是我希望根据我使用@DateTimeFormat注释设置的模式来格式化这个Date。

Can someone tells me, what am I doing wrong ? 有人能告诉我,我做错了什么? Many thanks, Ondrej. 非常感谢,Ondrej。

EDIT: I have the @InitBinder already, but it works only when I am creating a new object through the form, so it cast String to Date . 编辑:我已经有了@InitBinder ,但只有当我通过表单创建一个新对象时它才有效,所以它将StringDate But when I want to fill the form inputs with existing data in DB, it doesnt work. 但是当我想用DB中的现有数据填充表单输入时,它不起作用。

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy h:mm");
    sdf.setLenient(true);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

I am using Spring 3.1.1.RELEASE without Joda time, just ordinary java.util.Date. 我使用Spring 3.1.1.RELEASE而没有Joda时间,只是普通的java.util.Date。

<mvc:annotation-driven/> in dispatcher-servlet.xml is set properly. 在dispatcher-servlet.xml中正确设置了<mvc:annotation-driven/> When I use debugger I can see, that while creating the new object from form input values a method setAsText(String text) of CustomDateEditor is used. 当我使用调试器时,我可以看到,在从表单输入值创建新对象时,使用CustomDateEditor的方法setAsText(String text) I expect that the second method, String getAsText() will be used for formating Data to String while filling the form inputs. 我希望第二个方法String getAsText()将用于在填充表单输入时将Data格式化为String。 But, it doesnt ! 但是,它没有!

The @DateTimeFormat(pattern="dd.MM.yyyy hh:mm") annotation is basically saying that when you get a String in the particular pattern, convert it into @DateTimeFormat(pattern="dd.MM.yyyy hh:mm")注释基本上是说当你在特定模式中获得一个String时,将其转换为

java.util.Date , java.util.Calendar , java.long.Long , or Joda Time fields. java.util.Datejava.util.Calendarjava.long.Long或Joda Time字段。

In your case, it's a java.util.Date . 在你的情况下,它是一个java.util.Date

When you do something like 当你做类似的事情

<spring:message code="administration.record.created"/>:</form:label>

You're just calling toString() on the created Date object. 您只是在created Date对象上调用toString() The Date class internally uses its own format when returning a String in toString() and that's what you see. toString()返回一个String时, Date类在内部使用自己的格式,这就是你所看到的。

Consider using the fmt:formatDate tag from JSTL. 考虑使用JSTL中的fmt:formatDate标记。

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

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