1. 程式人生 > >使用thymeleaf框架,前臺日期格式是字符串,後臺不能以Date格式接收解決

使用thymeleaf框架,前臺日期格式是字符串,後臺不能以Date格式接收解決

tom style org property 方法 父類 sim imp java

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import java.text.SimpleDateFormat;
import java.util.Date;


Spring MVC 框架
公共父類controller類(ParentController )添加以下方法

/**
* 前臺日期格式轉換為Date格式
* 日期格式:yyyy-MM-dd,第二個參數為是否為空 true代表可以為空
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

使用thymeleaf框架,前臺日期格式是字符串,後臺不能以Date格式接收解決