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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19015581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:23:28  来源:igfitidea点击:

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

javahibernatespring-mvc

提问by s0vet

I am looking for solution with my current problem. I have a classic POJO mapped by Hibernate with Datevariable.

我正在寻找解决我当前问题的方法。我有一个由 Hibernate 用Date变量映射的经典 POJO 。

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

该实体保存在 MySQL 数据库中的 TIMESTAMP 变量 (2013-09-25 22:13:18.000) 中。

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. But I want to get this Date formated according the pattern, which I set using @DateTimeFormat annotation.

我的问题是,当我想编辑这个 POJO 并将其发送到 spring 形式以显示时,我得到的日期为 2013-09-25 22:13:18.000,与 MySQL 时间戳的格式完全相同。但是我想根据我使用 @DateTimeFormat 注释设置的模式来格式化这个日期。

Can someone tells me, what am I doing wrong ? Many thanks, Ondrej.

有人可以告诉我,我做错了什么吗?非常感谢,Ondrej。

EDIT: I have the @InitBinderalready, but it works only when I am creating a new object through the form, so it cast Stringto Date. But when I want to fill the form inputs with existing data in DB, it doesnt work.

编辑:我已经@InitBinder有了,但它仅在我通过表单创建新对象时才有效,因此它强制转换StringDate. 但是当我想用数据库中的现有数据填充表单输入时,它不起作用。

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

我正在使用没有 Joda 时间的 Spring 3.1.1.RELEASE,只是普通的 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 CustomDateEditoris 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 !

<mvc:annotation-driven/>在 dispatcher-servlet.xml 中设置正确。当我使用调试器我所看到的,而产生从表单输入新对象值的方法setAsText(String text)CustomDateEditor使用。我希望第二种方法String getAsText()将用于在填写表单输入时将数据格式化为字符串。但是,它没有!

回答by Sotirios Delimanolis

The @DateTimeFormat(pattern="dd.MM.yyyy hh:mm")annotation is basically saying that when you get a Stringin 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 时间字段。

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 createdDateobject. The Dateclass internally uses its own format when returning a String in toString()and that's what you see.

你只是叫toString()createdDate对象。在Date返回的字符串时类在内部使用它自己的格式toString(),这就是你所看到的。

Consider using the fmt:formatDatetag from JSTL.

考虑使用fmt:formatDate来自 JSTL的标记。