0
votes

Environment:

Spring 3.1.3.RELEASE

Spring webflow 2.3.0.RELEASE

I have posted this question recently on spring source forum. See: Spring webflow formatting issue

I followed the steps outlined in the reference documentation and the top answer in stackoverflow too:

Answer

I expect when my form submits and binding occurs, that a parse exception is thrown when entering in an invalid date value. But i do not see this happening.

Also if and when the exception is thrown, how do i handle it to display error message on the front end?

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.

I followed the steps and added into the servlet context

<!-- Enables controllers mapped with @RequestMapping annotations, formatting annotations @NumberFormat
    @DateTimeFormat, and JSR 303 style validation --> 
   <mvc:annotation-driven conversion-service="applicationConversionService1" />

<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" view-factory-creator="mvcViewFactoryCreator" development="true"/>

<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.

@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.

@DateTimeFormat(pattern="dd/MM/yyyy")
    private Date revisedTermExpiryDate = new Date();

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.

My objective is to register the plain DateFormatter with a global default date, parse strictly (lenient=false) and handle any parse exceptions gracefully. This to me is a great feature to have rather than handling parsing dates in validator for every web flow.

Thanks, Shane.

1
You wrote: "I have annotated a date field with the new dateformat @DateTimeFormat(pattern="dd/MM/yyyy") - So the class DateFormatter has lenient as false, so I expected when submitting the form input field, that a parse exception would occur if O entered for example the value '24/15/2012', but did not." -- But why do you expect this, the format is correct! - Ralph
Please post the question here, instead of only posting ONLY the link to an other forum. - Ralph
ralph cheers for the reply. That is not my question tho. Its not the format that is the issue. The formatter should parse strictly as lenient is false. ie the month value is not correct. i have done some digging and believe my formatter is not registered and as i have joda-time on my classpath its calling joda DateTimeFormatter. My goal is to parse front end input dates strictly and handle the exception gracefully in my application. - shane lee
Where does "dd/MM/yyyy" not match '24/15/2012'? - Ralph
Does 15 look like a valid month to you!? If you dont understand parsing strictly then please google it. - shane lee

1 Answers

0
votes

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:

  1. recordDecisionFormModel.revisedTermExpiryDate.type Mismatch
  2. revisedTermExpiryDate.typeMismatch
  3. java.util.Date.typeMismatch
  4. 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!!