Spring Transformer or property editor to convert multiple query parameters to an object?

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

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

An ideal solution would be if there was only one parameter - the field indicated "yyyy: MM: dd: hh: mm: aa", @DateTimeFormat, and WebDataBinder was set using the DefaultConversionService + joda-time class path.

But I have to keep the look and use this widget. In addition, this conversion in the handler method complicates validation. Any idea how to do this without worrying about the handler method?

+6
source share
1 answer

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

You can also use a custom WebArgumentResolver and have a special annotation, such as @InputDate , in your method signature.

+2
source

Source: https://habr.com/ru/post/892950/


All Articles