我在课堂上创建了一个时间戳成员并将其存储在数据库中。当我尝试更新它的值变为null但能够在UI中看到它的值。
我使用init binder将字符串转换为日期,但它不起作用,请帮帮我。
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;
}
}
控制器绑定
@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));
}
在UI中显示的日期
2015-02-11 08:35:18.0
这是我能够在UI中看到的日期格式,但是当我尝试更新其值在spring数据绑定对象中变为null时,请提前帮助我,谢谢。
答案 0 :(得分:1)
您使用的格式错误,当您声明month pattern时,您应该使用大写M
,因此它应该是
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat 也会将字符串转换为Date,因此不需要 initBinder ,您应该将其删除。
请注意,模式必须符合请求中日期的格式,发布的模式适合例如以下格式"2010-07-04 00:00:00"
,我提到这一点&#39;导致您的活页夹和模式 @DateTimeFormat 内部不同,您应该设置与您的请求参数格式对应的那个