Mark Estrada Mark Estrada - 1 year ago 136
Java Question

Customize Spring Error Message

I want to change the default error messages being thrown by Spring.

I basically have this form. Format of the date should follow "mm/dd/yyyy"

<label class="label">Start Date</label>
<form:input path="dteStartDate" />
<form:errors path="dteStartDate"/>

Everything works fine, but during binding I get this message added by the form:error tag.

Failed to convert property value of type [java.lang.String] to required type
[java.util.Date] for property dteStartDate; nested exception is java.lang.IllegalArgumentException:
Could not parse date: Unparseable date: "2010/11/19"

Can I customize this? So that instead of these I could show something like this:
Invalid Date Format. Format should be "mm/dd/yyyy"

This is Spring MVC 2.5. Any hints please help.

Answer Source

You need to define a MessageSource to resolve error code to the message. Error codes are built as described here (so you'll have typeMismatch.<objectName>.dteStartDate).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download