문제

I'm trying to register a global date format in my Spring MVC application. I have followed the documentation. The relevant part of my Spring config file looks like this:

<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" />

Based on the documentation, it is my understanding that this should use the configured DateFormatter for all my java.util.Date properties that I write out. However, when I just write out a date (of type java.util.Date in my JSP file like this:

Availability Date: ${model.availabilityDate}

It still writes out the date in the default SHORT format, effectively ignoring my default format.

I am aware that I can register @InitBinder methods, or specify formats with @DateTimeFormat, but that's not the point. I would like to change the global default to ensure consistent output of dates in specific format, unless specified otherwise.

I found other questions on SO similar to this one, however none seem to help me solve my problem:

도움이 되었습니까?

해결책

It seems the global default format only applies to marshalling of dates to and from input fields, and not when writing the date out directly using JSP.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top