Results 1 to 2 of 2

Thread: Issue with type conversion and date formatting

  1. #1
    Join Date
    Apr 2012
    Posts
    9

    Default Issue with type conversion and date formatting

    Environment:
    Spring 3.1.3.RELEASE
    Spring webflow 2.3.0.RELEASE

    I wanted to start using type conversions in my application. I followed the steps below for configuring type conversion formatting for spring MVC and SWF.

    http://static.springsource.org/sprin...ype-conversion

    I followed the steps and added into the servlet context
    Code:
     <!-- Enables controllers mapped with @RequestMapping annotations, formatting annotations @NumberFormat
        @DateTimeFormat, and JSR 303 style validation --> 
       <mvc:annotation-driven conversion-service="applicationConversionService1" />
    Code:
    <webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" view-factory-creator="mvcViewFactoryCreator" development="true"/>
    Code:
    <bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService"> 
         	<constructor-arg ref="applicationConversionService1"/> 
       </bean>
    I created a new class to register my own formatter for use in both Spring MVC and in Spring Web Flow.

    Code:
    @Component(value = "applicationConversionService1")
    public class ApplicationConversionServiceFactoryBean extends
    		FormattingConversionServiceFactoryBean {
    	
    	@Override
    	protected void installFormatters(FormatterRegistry registry) {
    	  // Register the default date formatter provided by Spring
    	  registry.addFormatter(new DateFormatter("dd/MM/yyyy"));
    	} 
    	
    }
    Now when i deploy to the server, the context is initialised correctly and classpath scanning registers the bean.

    In my form model that binds, i have annotated a date field with the new dateformat annotation.

    Code:
    @DateTimeFormat(pattern="dd/MM/yyyy")
    	private Date revisedTermExpiryDate = new Date();
    i know its the same format as the default but initially i want the type conversion to work before changing.

    So the class DateFormatter has lenient as false, so i expected when submitting the form input field, that a parse exception would occur if i entered for example the value '24/15/2012', but did not.

    Am i missing something here?

  2. #2
    Join Date
    Apr 2012
    Posts
    9

    Default

    One small update. I was debugging and as i have joda-time on my classpath i believe spring is registering joda DateTimeFormatter instead and the parsing is not failing.

    If i remove the annotation actually it calls DateFormatter which throws parse exception as expected but the exception is swallowed in the framework.

    UPDATE 2:
    i found why the exception was swallowed in our application, We were clearing the messages from the message context when validating.

    So If the date formatter throws an exception due to invalid value inputted by the user, spring handles this gracefully and creates four error message keys in the message context related to that field. For example if field revisedTermExpiryDate fails, four new keys will be created:
    recordDecisionFormModel.revisedTermExpiryDate.type Mismatch
    revisedTermExpiryDate.typeMismatch
    java.util.Date.typeMismatch
    typeMismatch

    All you need to do is specify in your messages.properties one of these keys with your validation message. Example:
    revisedTermExpiryDate.typeMismatch=Revised Term Expiry Date: DATE IS INVALID

    Solved!!
    Last edited by shanelee007; Jan 8th, 2013 at 10:53 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •