How to allow null date in Spring 3 MVC

故事扮演 提交于 2019-12-25 05:09:13

问题


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:


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>


来源:https://stackoverflow.com/questions/11118023/how-to-allow-null-date-in-spring-3-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!