我知道DateTimeFormat注释可以在Spring中的Model类的Date属性上定义,但是有什么方法可以为Spring Model类中的所有日期定义默认的dateTimeFormat注解吗?在春天设置默认的DateTimeFormat注释
@DateTimeFormat(图案= “MM/DD/YYYY”)
私人日期 deliveryDate;
这将使我无法注释每个日期字段。这也将使我稍后可以轻松更改应用程序的宽日期格式。
我知道DateTimeFormat注释可以在Spring中的Model类的Date属性上定义,但是有什么方法可以为Spring Model类中的所有日期定义默认的dateTimeFormat注解吗?在春天设置默认的DateTimeFormat注释
@DateTimeFormat(图案= “MM/DD/YYYY”)
私人日期 deliveryDate;
这将使我无法注释每个日期字段。这也将使我稍后可以轻松更改应用程序的宽日期格式。
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
正如kuhajeyan提到的,你可以使用粘合剂,而不是在控制器上,而是基于控制器的建议,将全局应用:
@ControllerAdvice
public class GlobalDateBinder {
/* Initialize a global InitBinder for dates*/
@InitBinder
public void binder(WebDataBinder binder) {
// here you can use kuhajeyan's code
}
}
您可以创建一个自定义注释有助于将日期解析为默认格式。但是,您必须至少在日期属性上添加自定义注释 –