Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Spring MVC Data Binding and user-friendly error messages

Thread view: 
cmilley2000@excite.com - 15 Jun 2006 16:17 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.