4

Is it possible to give Spring MVC's form binding some kind of type hint to indicate what class to instantiate and populate with form data?

I've got some quite unusual requirements to try and build a dynamic form, that represents a collection of different objects. The user can add objects from a selection of types to this collection, and then set the properties for that object type using form elements. I can figure out how to create the form using jQuery, but I'm not sure how to get Spring to handle a load of POST data when it won't know what types to bind to in advance.

1

1 Answer 1

3

One way that I can think of doing this is to write your own custom 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 indicate to Spring MVC, that your custom handler method argument resolver will be resolving the specific annotated method arguments(say @CustomType Object argument).

Once the call comes into the handler resolver, you can probably determine the type that the json request should map to, and call the json mapper with the actual type.

You can register a custom argument resolver this way:

<annotation-driven> <argument-resolvers> <beans:bean class="..CustomArgumentResolver"/> </argument-resolvers> </annotation-driven> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.