【问题标题】:Spring MVC ignores global date format?Spring MVC 忽略全局日期格式?
【发布时间】:2014-03-13 15:23:57
【问题描述】:

我正在尝试在我的 Spring MVC 应用程序中注册一个全局日期格式。我关注了the documentation。我的 Spring 配置文件的相关部分如下所示:

<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <beans:property name="registerDefaultFormatters" value="false" />
        <beans:property name="formatters">
            <beans:set>
                <beans:bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
            </beans:set>
        </beans:property>
        <beans:property name="formatterRegistrars">
            <beans:set>
                <beans:bean class="org.springframework.format.datetime.DateFormatterRegistrar">
                    <beans:property name="formatter">
                        <beans:bean class="org.springframework.format.datetime.DateFormatter">
                            <beans:property name="pattern" value="yyyyMMdd"/>
                        </beans:bean>
                    </beans:property>
                </beans:bean>
            </beans:set>
        </beans:property>
    </beans:bean>

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven conversion-service="conversionService" />

根据文档,据我了解,这应该为我写出的所有 java.util.Date 属性使用配置的DateFormatter。但是,当我在我的 JSP 文件中写出一个日期(类型为 java.util.Date 时,如下所示:

Availability Date: ${model.availabilityDate}

它仍然以默认的SHORT 格式写出日期,实际上忽略了我的默认格式。

我知道我可以注册@InitBinder 方法,或使用@DateTimeFormat 指定格式,但这不是重点。除非另有说明,否则我想更改全局默认值以确保以特定格式输出一致的日期。

我在 SO 上发现了与此类似的其他问题,但似乎没有一个可以帮助我解决我的问题:

【问题讨论】:

    标签: java spring spring-mvc datetime-format


    【解决方案1】:

    似乎全局默认格式仅适用于输入字段之间的日期编组,而不适用于直接使用 JSP 写出日期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 2013-04-09
      • 2013-02-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多