在Spring中设置默认的DateTimeFormat注释

在Spring中设置默认的DateTimeFormat注释

本文介绍了在Spring中设置默认的DateTimeFormat注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以在Spring的Model类的Date属性上定义DateTimeFormat批注,但是有什么方法可以为Spring Model类中的所有Date定义默认的dateTimeFormat批注?

I know that a DateTimeFormat annotation can be defined on Date properties of a Model class in Spring but is there any way of defining a default dateTimeFormat annotation for all Dates in Spring Model classes?

这将使我免于注释每个日期字段。

It will save me from annotating each date field. It will also make it easy for me to change the application wide date format later on.

推荐答案

正如kuhajeyan所述,您可以使用活页夹,而不是在控制器上,而是在控制器建议上,它将在全球范围内应用:

As kuhajeyan mentioned, you can use binder, but not on a controller, rather on controller advice, that will apply it globally:

@ControllerAdvice
public class GlobalDateBinder {

 /* Initialize a global InitBinder for dates*/

 @InitBinder
 public void binder(WebDataBinder binder) {
  // here you can use kuhajeyan's code
 }
}

这篇关于在Spring中设置默认的DateTimeFormat注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:39