Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Programmatically bind object from request

Is there a way to programmatically bind form data to an object knowing the class type? I thought there would be something like

T instance = something.pleaseDoSomeMagicBind(class, request)

somewhere or something similar, but I had no luck so far.

Thank you

like image 816
Matteo Rolla Avatar asked Jul 25 '14 15:07

Matteo Rolla


1 Answers

Thanks to Sotirios (you saved my sanity) hint, I've been able to achieve what I've been looking for and I'm leaving here my findings if anyone else is interested

final WebDataBinder binder = new WebDataBinder(BeanUtils.instantiate(clazz));
ServletRequestParameterPropertyValues values = new ServletRequestParameterPropertyValues(request);
binder.bind(values);
final BindingResult result = binder.getBindingResult();
like image 69
Matteo Rolla Avatar answered Oct 22 '22 00:10

Matteo Rolla