无法使用initbinder spring将日期转换为所需格式

时间:2018-11-18 18:06:29

标签: spring-mvc

我无法使用initbinder转换日期格式。 这是我的jsp代码。

<!DOCTYPE html>
 <html>
  <head>
  </head>
           <form action="postdata" method="post">
           fname <input type="text" name="fname"/> </br> 
           lname <input type="text" name="lname"/> </br>
           Date <input type="text" name="myDate"/> </br>
           <input type="submit"/>
          </form>   
   </body>
   </html>

我的模型类Employee.java

 package com.model;
 import java.util.Date;

 public class Employee
    {
      private String fname;
      private String lname; 
      private Date myDate;

     public String getFname() {
        return fname;
     }

    public void setFname(String fname) {
      this.fname = fname;
    }

    public String getLname() {
    return lname;
   }

  public void setLname(String lname) {
   this.lname = lname;
  }

  public Date getMyDate() {
    return myDate;
 }

public void setMyDate(Date myDate) {
  this.myDate = myDate;
}

 @Override
public String toString() {
  return "Employee [fname=" + fname + ", lname=" + lname + ", myDate=" + 
  myDate + "]";
  }

}

 My Controller is as below

package com.mkyong;

  import java.io.IOException;
  import java.util.Date;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Map;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.beans.propertyeditors.CustomDateEditor;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.servlet.ModelAndView;
 import com.model.Employee;

 @Controller
 public class WelcomeController {

@InitBinder
public void dataBinding(WebDataBinder binder) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, "myDate", new 
    CustomDateEditor(dateFormat, false));
  }


@RequestMapping(value="/postdata",method=RequestMethod.POST)
public ModelAndView welcome(HttpServletRequest request, HttpServletResponse 
 response,@ModelAttribute("employee") Employee employee,BindingResult 
  bresult)
{
    System.out.println("In Welcomeontroller");
    System.out.println(employee);

   return new ModelAndView("myajax");

}

}

当我在调试模式下检查时,在dataBinding方法中,我正在获取Employee模型的空值。在欢迎方法中,我将日期格式设置为IST 1990年1月1日星期一00:00:00。我希望它采用dd / MM / yyyy格式。请帮帮我,我需要在JQGrid的项目中应用相同的内容。

1 个答案:

答案 0 :(得分:0)

更改

binder.registerCustomEditor(Date.class, "myDate", new CustomDateEditor(dateFormat, false));

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

如果您想使用一种比上一种方法更简单的方法,请尝试如下注释日期字段。

@DateTimeFormat(pattern = "dd/MM/yyyy")
@ModelAttribute
@Temporal(TemporalType.DATE)
@Column(name = "date")
private Date date;