Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Convertor or Property editor for conversion of multiple request parameters into an Object?

Concrete example would be implementation of a Convertor/PropertyEditor for (2011, 11, 24, 8, 10, AM) request parameters to Date object ?

Let say that my UI uses a widget that uses 6 html form fields for calendar (2011, 11, 24, 8, 10, AM) - which is very inconvenient ..

Ideal solution would be if the parameter was only one - "yyyy:MM:dd:hh:mm:aa", @DateTimeFormat annotation was on the field and the WebDataBinder has been set with the DefaultConversionService + joda-time is on the classpath.

But I have to keep look and feel and use that widget. Also doing that conversion in handler method complicates validation a lot. Any idea how to do that without bothering in handler method ?

like image 480
lisak Avatar asked Jul 17 '11 19:07

lisak


1 Answers

If you create a custom wrapper object that have fields for each of the input parameters, having public ... handlerMethod(InputDate date) will populate it. Then, in the same class, you can have toDateTime() which will construct a DateTime based on the input.

You can also use a custom WebArgumentResolver and have a custom annotation, for example @InputDate, in your method signature.

like image 183
Bozho Avatar answered Oct 11 '22 01:10

Bozho