[英]Setting default DateTimeFormat Annotation in Spring
我知道可以在Spring的Model類的Date屬性上定義DateTimeFormat批注,但是有沒有辦法為Spring Model類中的所有Date定義默認的dateTimeFormat批注?
@DateTimeFormat(pattern =“ MM / dd / YYYY”)
私人日期交貨日期;
這將使我免於注釋每個日期字段。 以后,我也可以輕松更改應用程序寬日期格式。
正如kuhajeyan所提到的,您可以使用活頁夾,但是不能在控制器上使用,而是在控制器建議上使用,它將在全球范圍內應用它。
@ControllerAdvice
public class GlobalDateBinder {
/* Initialize a global InitBinder for dates*/
@InitBinder
public void binder(WebDataBinder binder) {
// here you can use kuhajeyan's code
}
}
在您的控制器中
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.