【问题标题】:Set date format for an input text using Spring MVC使用 Spring MVC 为输入文本设置日期格式
【发布时间】:2012-05-28 13:00:28
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

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

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

    然后数据编辑器本身可以如下所示:

    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());
     }
    }
    

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

    【讨论】:

    • 谢谢!这绝对比我的解决方案好。
    • @davioooh Spring 3.0 + ?这是我认为static.springsource.org/spring/docs/current/…“使用属性注册器”的相关章节,展示了如何在全球范围内进行
    • 是的,我使用的是 Spring 3.1,但我还是新手……(以及一般的 Spring 框架……)
    • 这里如何设置全局编辑器:stackoverflow.com/q/1268021/1061499非常有帮助!
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      步骤

      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.创建一个带有@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. 在您的 Spring MVC 配置文件(例如 webmvc-config.xml)中添加允许 Spring 扫描您在其中创建 GlobalBindingInitializer 类的包的行。例如,如果您在 org.example.common 包中创建了 GlobalBindingInitializer:

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

      完成!

      来源:

      【讨论】:

        猜你喜欢
        • 2018-02-10
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 1970-01-01
        相关资源
        最近更新 更多