How to allow null date in Spring 3 MVC

2019-09-06 18:06发布

I've defined a global formatter for date bindings:

<annotation-driven conversion-service="conversionService" />

<beans:bean id="conversionService"
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <beans:property name="converters">
        <beans:list>
        ...
        </beans:list>
    </beans:property>
    <beans:property name="formatters">
        <beans:set>
            <beans:bean id="dateFormatter"
                class="org.springframework.format.datetime.DateFormatter">
                <beans:constructor-arg><beans:value>dd/MM/yyyy</beans:value></beans:constructor-arg>
            </beans:bean>
        </beans:set>
    </beans:property>
</beans:bean>

This is working fine but now I also need that a particular date field can be left blank. How can I configure the formatter (or this very field) to accept null value?

Thanks! :)

1条回答
Animai°情兽
2楼-- · 2019-09-06 18:57

You can do this by set lenient in false for DateFormatter

<mvc:annotation-driven conversion-service="conversionService"/>

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>
            <bean class="org.springframework.format.datetime.DateFormatter">
                <constructor-arg>
                    <value>dd/MM/yyyy</value>
                </constructor-arg>
                <property name="lenient" value="false"/>
            </bean>
        </set>
    </property>
</bean>
查看更多
登录 后发表回答