Dear Spring MVC experts!
I am using a controller of type AbstractWizardFormController to
implement a multi-page form entry workflow. This controller operates
on a session-scoped command object which has a property of type
java.util.Date. On page 3 of the wizard, the user enters a date into a
textbox which will be bound to a java.util.Date property of the command
object.
In my controller, I am registering a custom editor as follows:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
The data binding process is behaving as I would expect. If the user
types an invalid date into the textbox, then the field cannot be bound
and the framework adds a FieldError to the Errors object with code
"typeMismatch". Unfortunately, this FieldError results in a very
un-user-friendly error message being put on the screen. The text of
the message is:
"Failed to convert property value of type [java.lang.String] to
required type [java.util.Date] for property billDate; nested exception
is java.lang.IllegalArgumentException: Could not parse date:
Unparseable date: "xyz" "
It is not at all clear to me how to transform this ugly error message
to a user-friendly message. I can think of a number of possible ways
of doing this, but all seem overly-complex. Is there a standard way to
map data-binding errors to user-friendly messages? Thanks in advance
for your help.
Regards,
Craig
liujian.mail@gmail.com - 25 Jun 2006 03:52 GMT
Have the customized typeMismatch message in your message properties
file that Spring load as messageResource.
Example:
# For generic error message on type mismatch Date
typeMismatch.java.util.Date={0} is an invalid date.
# For specific error message on perticular form field
# (e.g. startDt is a form field name)
typeMismatch.startDt=Start Date is an invalid date.
Cheers,
Liu Jian
> Dear Spring MVC experts!
>
[quoted text clipped - 34 lines]
> Regards,
> Craig
javadude - 27 Jun 2006 18:09 GMT
Thanks for the info. Where (online/book) is this documented?
> Have the customized typeMismatch message in your message properties
> file that Spring load as messageResource.
[quoted text clipped - 48 lines]
> > Regards,
> > Craig