简体   繁体   中英

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.

@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).

I am using Spring form to show data saved in my 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. But I want to get this Date formated according the pattern, which I set using @DateTimeFormat annotation.

Can someone tells me, what am I doing wrong ? Many thanks, 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 . But when I want to fill the form inputs with existing data in DB, it doesnt work.

@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.

<mvc:annotation-driven/> in dispatcher-servlet.xml is set properly. 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. I expect that the second method, String getAsText() will be used for formating Data to String while filling the form inputs. 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

java.util.Date , java.util.Calendar , java.long.Long , or Joda Time fields.

In your case, it's a 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. The Date class internally uses its own format when returning a String in toString() and that's what you see.

Consider using the fmt:formatDate tag from JSTL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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