jaya sankar
jaya sankar

Reputation: 39

Spring MVC date binding returns null value

I created one time-stamp member in class and storing it in database. When I try to update its value becomes null but able to see its value in UI.

I used init binder for conversion of string to date but it is not working, please help me.

CNTC.java

    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;

    import static javax.persistence.GenerationType.IDENTITY;

    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import org.springframework.format.annotation.DateTimeFormat;

    /**
     * Cntc generated by hbm2java
     */
    @Entity
    @Table(name = "cntc", catalog = "ecable")
    public class Cntc implements java.io.Serializable {

        private Integer cntcId;
        private String mobNo;
        private String altMobNo;
        private String emailId;
        private String crtId;
        private Date crtTs;
        private String updId;
        @DateTimeFormat(pattern="yyyy-mm-dd HH:mm:ss.z")
        private Date updTs;
        private Set<Addr> addrs = new HashSet<Addr>(0);



        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "CNTC_ID", unique = true, nullable = false)
        public Integer getCntcId() {
            return this.cntcId;
        }

        public void setCntcId(Integer cntcId) {
            this.cntcId = cntcId;
        }

        @Column(name = "MOB_NO", nullable = false, length = 500)
        public String getMobNo() {
            return this.mobNo;
        }

        public void setMobNo(String mobNo) {
            this.mobNo = mobNo;
        }

        @Column(name = "ALT_MOB_NO", nullable = false, length = 500)
        public String getAltMobNo() {
            return this.altMobNo;
        }

        public void setAltMobNo(String altMobNo) {
            this.altMobNo = altMobNo;
        }

        @Column(name = "EMAIL_ID", nullable = false, length = 1000)
        public String getEmailId() {
            return this.emailId;
        }

        public void setEmailId(String emailId) {
            this.emailId = emailId;
        }

        @Column(name = "CRT_ID", nullable = false, length = 200)
        public String getCrtId() {
            return this.crtId;
        }

        public void setCrtId(String crtId) {
            this.crtId = crtId;
        }

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "CRT_TS", nullable = false, length = 0)
        public Date getCrtTs() {
            return this.crtTs;
        }

        public void setCrtTs(Date crtTs) {
            this.crtTs = crtTs;
        }

        @Column(name = "UPD_ID", length = 200)
        public String getUpdId() {
            return this.updId;
        }

        public void setUpdId(String updId) {
            this.updId = updId;
        }

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "UPD_TS", length = 0)
        public Date getUpdTs() {
            return this.updTs;
        }

        public void setUpdTs(Date updTs) {
            this.updTs = updTs;
        }

    }

Controller binding

    @InitBinder
    public final void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

Date displayed in UI

2015-02-11 08:35:18.0

This is date format I am able to see in UI but when I try to update its value becomes null in spring data binding object, please help me, thanks in advance.

Upvotes: 1

Views: 3569

Answers (1)

Master Slave
Master Slave

Reputation: 28519

The format that you're using is wrong, when you're declaring a month pattern you should be using capitalized M, so it should be

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")

the @DateTimeFormat will also convert a string to Date, so your initBinder is not needed and you should delete it.

Note that the pattern must fit the format of the date in the request, the posted pattern fits for example a following format "2010-07-04 00:00:00", I'm mentioning this 'cause your binder and the pattern inside the @DateTimeFormat are different, you should set the one corresponding to your request parameter format

Upvotes: 1

Related Questions