Packing multiple parameters into one inside Spring MVC Controller - spring

Is there an easy way to pack multiple parameters from the URL into a single unit and send it to a Spring MVC Controller?
I have more than 20 parameters and it is inconvenient to type #RequestParam for each of them.
Also, how we can I handle a varying number of parameters, e.g if I have some list of info that varies in size?

This is what Spring's MVC's #ModelAttribute annotation is used for. You can place all your parameters on a bean and move that around instead.
When you submit your form, Spring binds the request parameters into your bean and it works also with list parameters.

Related

(How) can a Spring <form: tag handle multiple Model Attributes

After reading related questions, such as:
Spring MVC form handling form multiple pojos
Spring MVC: Having multiple #ModelAttribute in form handling action
I still wonder how could a Spring MVC <form: tag handle multiple modelAttributes. In the answers to the questions above, they either use JSON ( Why?) or use some tricks in the name of the raw input HTML tags.
Could somebody post a minimal, working example here ( two Pojos, a Controller and a view)?

How to change path parameters before passing to controllers in spring?

What is the best way to change path parameter (may be using interceptor) before passing it to controllers ? I am using spring mvc.
Right now, i am adding an extra parameter to get the job done, and checking for that in the controller, but i would rather change the parameter and not change anything in the controller

How to get the original handler URI (with wildcards, etc.) in a HandlerInterceptor in Spring?

I am collecting custom metrics for my controller endpoints via HandlerInterceptor. In postHandle I have everything I need and I would like to save the metrics for my endpoints along with the original route defined in the controller, so not the actual route filled with the data.
E.g. I have #GetMapping("/cats/{name}") and make GET request on /cats/tom I still want the defined route "cats/{name}"
I found that I can do this with the Object handler which is the parameter of postHandle -> I can cast it to HandlerMethod get the annotations of the method and find it from memberValues. This seems a bit overkill I also have to filter for Get-, Post-, Put-, Delete-, Patch-, RequestMapping annotations. Is there any easier or more straightforward way to do this? If so how? If not what else do I have to take into consideration with this solution?
It turns out Spring adds this data to the HttpServletRequest attributes. So it can be retrieved using:
String routePattern = (String) request.getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern");

Polymorphic Form Binding in Spring MVC

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.
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>

How can I bind fieldName_1, fieldName_2 to a list in spring mvc

I'm trying to convert a struts 1 application to Spring MVC 3.0. We have an form with quite a few parameters, most of which where automatically binded in struts. However there where a number of fields in the format fieldName_# where # is a number that we manually bound by looping through the request.
I'm looking for a tidier way to do this in Spring mvc, but don't know where to start.
Ideally we should have them as fieldName[#] and it would be easier, but we cannot change this and have to keep the fieldName_# format. Also the number of these fields that are sent in the request are unknown.
One way to achieve this is by wrapping the servletRequest and implementing the getParameter (and associated methods) such a way that parameters with name fieldName_# are returned as fieldName[#]. A servletFilter would be one option to wrap the request.

Resources