Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Spring MVC Data Binding and user-friendly error messages

Reply
Thread Tools

Spring MVC Data Binding and user-friendly error messages

 
 
cmilley2000@excite.com
Guest
Posts: n/a
 
      06-15-2006
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

 
Reply With Quote
 
 
 
 
liujian.mail@gmail.com
Guest
Posts: n/a
 
      06-25-2006
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


http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
javadude
Guest
Posts: n/a
 
      06-27-2006
Thanks for the info. Where (online/book) is this documented?


(E-Mail Removed) 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
>
>
> (E-Mail Removed) 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


 
Reply With Quote
 
DaveHarris DaveHarris is offline
Junior Member
Join Date: Oct 2007
Posts: 1
 
      10-17-2007
Hi Guys,

I read your thread with interest, as "typeMismatch.java.util.Date={0} is an invalid date" is exactly what I want. The only problem I have however is that when {0} is expanded out, it replaces that with the bound field name, which is usually a non-user friendly name like 'startDate'.

I want to be able to put my own string in there like I can do in my own form validator:
error.rejectValue("numAxles", "validation.error.field.numberMismatch", new Object[] {"The number of axles"}, null); where validation.error.field.numberMismatch={0} must be a number

Is there a way to control the parameter sent to {0} while in the spring binding process. While I can just use my own validator, it seems kindof silly to re-invent the wheel when its something that spring provides us, I just want to configure it.

Cheers and thanks for any help in advance
Dave
 
Reply With Quote
 
andrei001 andrei001 is offline
Junior Member
Join Date: Nov 2007
Posts: 1
 
      11-30-2007
I have a similar problem, and perhaps someone here knows the answer. I am using a date field in a search page, binding it using a customDateEditor, and using a validator for correct formatting. The problem i have is this: i am using the <spring:bind> tag so that the page will return with the value that caused a validation fault. But even if the page succeeds it is submitted back onto itself, this time carrying the search results. But this time the date field will return in a completly different format. I cannot use the <fmt:formatDate> tag because of <spring:bind>. How can i register a "reverse" bind editor if you will, something that will print my data according to a specific format depending on the class?

Code snippet follows :
<spring:bind path="filesRequest.fileEndDateSearch">
<input type="text" id="fileEndDateSearch" name="fileEndDateSearch" class="txtBox" value="<fmt:formatDate value="${status.value}" type="date" pattern="yyyy-MM-dd" />"/>
</spring:bind>

Using <fmt:formatDate> for the ${status.value} will cause an exception in case the validator fails, as the format date tag will try to parse the erroneous input.
 
Reply With Quote
 
jenueheightz jenueheightz is offline
Junior Member
Join Date: Mar 2008
Posts: 1
 
      03-07-2008
If we put the key-value pair for all the labels for the fields into messages.properties, we can make it more user friendly.

label.numAxles=The number of axles
validation.error.field.numberMismatch={0} must be a number

errors.rejectValue("numAxles", "validation.error.field.numberMismatch",
new Object[]{ new DefaultMessageSourceResolvable(new String[]{"label.numAxles"})},"");


Passing the key "label.numAxles" to the {0} would result to a message like "The number of axles must be a number". Not passing a fieldname to the {0} which is "numAxles" which results to "numAxles must be a number" which is not more user friendly.


-jenue
 

Last edited by jenueheightz; 11-14-2009 at 09:53 AM..
Reply With Quote
 
rishabh2u rishabh2u is offline
Junior Member
Join Date: Nov 2009
Posts: 1
 
      11-11-2009
This is a nice and neat feature I feel. I used below to parse a check box in a form into a boolean field in Command.

Code:
	/**
	 * This is an overridden verison of initBinder method of Spring baseCommandController.
	 * It is used by Spring to register a custom editor for a String field coming in a 
	 * Http request. This lets you have any datatype in your command and not just String.
	 */
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
	}
 
Reply With Quote
 
ahsan_cse2004 ahsan_cse2004 is offline
Junior Member
Join Date: Sep 2010
Posts: 2
 
      09-21-2010
Quote:
Originally Posted by andrei001
I have a similar problem, and perhaps someone here knows the answer. I am using a date field in a search page, binding it using a customDateEditor, and using a validator for correct formatting. The problem i have is this: i am using the <spring:bind> tag so that the page will return with the value that caused a validation fault. But even if the page succeeds it is submitted back onto itself, this time carrying the search results. But this time the date field will return in a completly different format. I cannot use the <fmt:formatDate> tag because of <spring:bind>. How can i register a "reverse" bind editor if you will, something that will print my data according to a specific format depending on the class?

Code snippet follows :
<spring:bind path="filesRequest.fileEndDateSearch">
<input type="text" id="fileEndDateSearch" name="fileEndDateSearch" class="txtBox" value="<fmt:formatDate value="${status.value}" type="date" pattern="yyyy-MM-dd" />"/>
</spring:bind>

Using <fmt:formatDate> for the ${status.value} will cause an exception in case the validator fails, as the format date tag will try to parse the erroneous input.
Hi,
I am sorry to resurrect this old thread, but did you find the solution to your problem. I have the same issue. I am setting the dates from my controller class and it is not visible on UI. I have already setup CustomDateEditor for Date class.

Thanks
Ahsan
 
Reply With Quote
 
ahsan_cse2004 ahsan_cse2004 is offline
Junior Member
Join Date: Sep 2010
Posts: 2
 
      09-21-2010
I did it as follows
Code:
<spring-tags:bind path="startDate">
       <input type="text" value="<fmt:formatDate value="${startDate}"  type="date" pattern="yyyy-MM-dd"/>" id="impressionReportStartDate" name="startDate"/>
</spring-tags:bind>
Where startDate is coming from my server code. Somehow I had to use name="startDate" also because path="startDate" alone did not work for me.

Thanks
Ahsan
 
Reply With Quote
 
Naga Syamala A Naga Syamala A is offline
Junior Member
Join Date: Feb 2012
Posts: 1
 
      02-17-2012
Quote:
Originally Posted by ahsan_cse2004 View Post
I did it as follows
Code:
<spring-tags:bind path="startDate">
       <input type="text" value="<fmt:formatDate value="${startDate}"  type="date" pattern="yyyy-MM-dd"/>" id="impressionReportStartDate" name="startDate"/>
</spring-tags:bind>
Where startDate is coming from my server code. Somehow I had to use name="startDate" also because path="startDate" alone did not work for me.

Thanks
Ahsan

Hi All,

I am also getting same type of probelm.

I am using date format is MM/dd/yyyy HH:mm

In my controller CustomDateEditorcode is like given below

@InitBinder protected void initBinder(final WebDataBinder dataBinder) {
final SimpleDateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy HH:mm");
dataBinder.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormat, true));
}

and in my velocity file the code is lke this:

<input type="text" id="start" name="start" value="$!date.format('MM/dd/yyyy H:m',$!campaign.start)" size="30" class='field text small #if($status.error) error #end' tabindex="4" />

If user entered date as : 19/02/2012 11:53 , then it is not ging any error or exception simply accepting and taken as Tue Jul 02 11:53:00 IST 2013.

Can anybody tell me the way how to strict the month value to maximum 12.

Thanks,
Syamala.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spring Python 0.8.0 has been released/Spring Python goes green Goldfish Python 1 11-06-2008 06:12 PM
differences between Spring WebFlow,Spring MVC,and String Portlet MVC? rmn190 Java 2 01-10-2008 02:27 AM
How to reach HTTPSession from Spring application (non-MVC)? Azo80 Java 0 04-11-2007 09:05 AM
Help with Spring MVC XSLT view dustin.whitney@gmail.com Java 1 05-02-2006 10:40 PM
Simple URL Handling not working with Spring MVC Kannan Java 1 05-17-2005 08:58 AM



Advertisments