java 使用 Spring MVC 为输入文本设置日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10785107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:33:47  来源:igfitidea点击:

Set date format for an input text using Spring MVC

javaspringspring-mvc

提问by davioooh

How can I set the format for a Datein a text field with Spring MVC?

如何Date使用 Spring MVC 在文本字段中设置 a 的格式?

I'm using the Spring Form tag library and the inputtag.

我正在使用 Spring Form 标签库和input标签。

What I get now is something like this Mon May 28 11:09:28 CEST 2012.

我现在得到的是这样的Mon May 28 11:09:28 CEST 2012

I'd like to show the date in dd/MM/yyyyformat.

我想以dd/MM/yyyy格式显示日期。

回答by NimChimpsky

register a date editor in yr controller :

在 yr 控制器中注册一个日期编辑器:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LocalDate.class, new LocalDateEditor());
}

and then the data editor itself can look like this :

然后数据编辑器本身看起来像这样:

public class LocalDateEditor extends PropertyEditorSupport{

 @Override
 public void setAsText(String text) throws IllegalArgumentException{
   setValue(Joda.getLocalDateFromddMMMyyyy(text));
 }

 @Override
 public String getAsText() throws IllegalArgumentException {
   return Joda.getStringFromLocalDate((LocalDate) getValue());
 }
}

I am using my own abstract utility class (Joda) for parsing dates, in fact LocalDates from Joda Datetime library- recommended as the standard java date/calendar is an abomination, imho. But you should get the idea. Also, you can register a global editor, so you don't have to do it each controller (I can't remember how).

我正在使用我自己的抽象实用程序类 (Joda) 来解析日期,实际上是来自Joda Datetime 库的LocalDates - 推荐作为标准的 java 日期/日历是可憎的,恕我直言。但你应该明白。此外,您可以注册一个全局编辑器,因此您不必每个控制器都这样做(我不记得是怎么做的)。

回答by davioooh

Done! I just added this method to my controller class:

完毕!我刚刚将此方法添加到我的控制器类中:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

回答by Kurt Bourbaki

If you want to format all your dates without having to repeat the same code in every Controller, you can create a global InitBinderin a class annotated with @ControllerAdviceannotation.

如果您想格式化所有日期而不必在每个 Controller 中重复相同的代码,您可以在使用@ControllerAdvice注释的类中创建一个全局 InitBinder

Steps

脚步

1.Create a DateEditor class that will format your dates, like this:

1.创建一个 DateEditor 类来格式化您的日期,如下所示:

    public class DateEditor extends PropertyEditorSupport {

    public void setAsText(String value) {
      try {
        setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
      } catch(ParseException e) {
        setValue(null);
      }
    }

    public String getAsText() {
      String s = "";
      if (getValue() != null) {
         s = new SimpleDateFormat("dd/MM/yyyy").format((Date) getValue());
      }
      return s;
    }

2.Create a class annotated with @ControllerAdvice (I called it GlobalBindingInitializer):

2.创建一个用@ControllerAdvice 注解的类(我称之为 GlobalBindingInitializer):

    @ControllerAdvice
    public class GlobalBindingInitializer {

     /* Initialize a global InitBinder for dates instead of cloning its code in every Controller */

      @InitBinder
      public void binder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateEditor());
      }
    }

3.In your Spring MVC configuration file (for example webmvc-config.xml) add the lines that allow Spring to scan the package in which you created your GlobalBindingInitializer class. For example, if you created GlobalBindingInitializer in the org.example.common package:

3.在您的 Spring MVC 配置文件(例如 webmvc-config.xml)中添加允许 Spring 扫描您在其中创建 GlobalBindingInitializer 类的包的行。例如,如果您在 org.example.common 包中创建了 GlobalBindingInitializer:

    <context:component-scan base-package="org.example.common" />

Finished!

完成的!

Sources:

资料来源: