Polymorphic binding in Spring MVC

Is it possible to provide a Spring MVC form with some kind of tooltip binding to indicate which class should be created and populated with form data?

I have some rather unusual requirements to try to build a dynamic form representing a collection of different objects. A user can add objects from a set of types to this collection, and then set properties for this type of object using form elements. I can figure out how to create a form using jQuery, but I'm not sure how to get Spring to handle loading POST data when it doesn't know which types to bind in advance.

+4
source share
1 answer

One of the ways I can come up with is to write my own HandlerMethodArgumentResolver , which is responsible for translating the request into the argument values ​​of the controller methods. You should be able to create a custom annotation that will point to Spring MVC that your argument handler of your custom handler will handle the specific arguments of the annotated method (say @CustomType Object argument ).

As soon as the call hits the handler’s handler, you can probably determine the type that the json request should display and call json-mapper with the actual type.

You can register your own argument converter as follows:

 <annotation-driven> <argument-resolvers> <beans:bean class="..CustomArgumentResolver"/> </argument-resolvers> </annotation-driven> 
+3
source

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


All Articles