aspose file tools*
The moose likes Spring and the fly likes MVC 2.5 - Help with a custom PropertyEditor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "MVC 2.5 - Help with a custom PropertyEditor" Watch "MVC 2.5 - Help with a custom PropertyEditor" New topic
Author

MVC 2.5 - Help with a custom PropertyEditor

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi,
I wonder if anyone can help.
I am putting together a Spring MVC 2.5 app, based around a form backing object which I have no control over (its based on an already established schema)
For the most part this is coming together nicely, but I am now stuck with how to populate a couple of properties. Within the main form backing object (which represents an insurance quote), there is a property for a previous claim. Within this there is a property representing the claim date. This property is a javax.xml.datatype.XMLGregorianCalendar which in turn has int properties for day, month, year etc. I guess it looks something like this:

Within my view JSP I have a couple of html SELECT drop down fields to allow the user to specify the claim month and year. Here is what I do with year (but month is the same):

The list ${claimYears} has id's (ie. the value that is submitted back) of 2006, 2007, 2008, 2009 and 2010

The problem comes when the user does not fill out a claim month and year (they dont have to, they are not required fields). Because of my "Please choose" option, an empty string value is submitted, which spring tries to bind to an int.

This much I understand, and I also think I understand that I need to write a PropertyEditor. I've written this:

which I think will do the job (ie. only attempt to bind if the value is not null and not an empty string); and I've tried to configure it into my controller like this:

but I think this is where its wrong and I'm getting stuck. When the view is requested via a GET (ie. before I attempt to POST it back), I now get this exception:


My gut feel is that its something to do with the way I am registering my custom property editor, in that I am registering it for XMLGregorianCalendar but actually it should be for the int (primitive type) within XMLGregorianCalendar

Or am I barking up the wrong tree??

Any help with this very very much appreciated;
Cheers

Nathan
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
I've kind of fixed this by writing a property editor for int instead of XMLGregorianCalendar:
So, in my controller I do this:

(didn't know you could do int.class - I thought it was a primitive )
And then my property editor looks like this:

This works, but I'm not entirely sure I like it as now all int fields on this page will be using this property editor.
Would be interested to know how anyone else might go about solving this one?

Cheers

Nathan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: MVC 2.5 - Help with a custom PropertyEditor