简体   繁体   English

如何将 spring 字段中的日期绑定到 pojo 类

[英]how to bind date in spring field to pojo class

  1. I wanted to bind the spring date field to the pojo class and return the date我想将 spring 日期字段绑定到 pojo 类并返回日期
  2. The dateOfbirth is of type java.sql.Date . dateOfbirth 是 java.sql.Date 类型。 Now when i pass the date from spring form to the function setDateOfBirth ,it is taking null value现在,当我将日期从弹簧形式传递给函数 setDateOfBirth 时,它采用空值

For example in register.jsp (Registration form) I have the particular field例如在register.jsp (注册表单)中,我有特定的字段

<form:label path="DateOfBirth">DateOfBirth</form:label>
<form:input path="DateOfBirth" type="date"/>

Register.java (Pojo class) Register.java (Pojo 类)

 public void setDateOfBirth(Date dateOfBirth) {
   this.dateOfBirth = dateOfBirth   ;
 }

 public void getDateOfBirth() {
   return this.dateOfBirth;
 }

when I call getDateOfBirth()当我调用getDateOfBirth()
returns: Sat Dec 12 00:00:00 IST 1992返回: Sat Dec 12 00:00:00 IST 1992

Input :- 12/12/1992

Expected Output :- 12/12/1992预期Output :- 12/12/1992

What I get: null我得到的: null

Please help me how to convert this ?请帮我如何转换这个?

Try to add @DateTimeFormat(pattern = "dd/MM/yyyy") to your dateOfBirth in Register.java尝试将@DateTimeFormat(pattern = "dd/MM/yyyy")Register.javadateOfBirth

@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date dateOfBirth;

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

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