Spring MVC Data Binding and user-friendly error messages  
Author Message
cmilley2000





PostPosted: 2006-6-15 23:17:00 Top

java-programmer, Spring MVC Data Binding and user-friendly error messages 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





PostPosted: 2006-6-25 10:52:00 Top

java-programmer >> Spring MVC Data Binding and user-friendly error messages 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


email***@***.com wrote:
> 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

 
javadude





PostPosted: 2006-6-28 1:09:00 Top

java-programmer >> Spring MVC Data Binding and user-friendly error messages Thanks for the info. Where (online/book) is this documented?


email***@***.com wrote:
> 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
>
>
> email***@***.com wrote:
> > 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