Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

MVC 2.5 - Help with a custom PropertyEditor

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic