How to change path parameters before passing to controllers in spring? - 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

Related

Spring Controller Url

I have question about controllers. Always when i working with controller im start to declare #RequestMapping for example if have UserController then is #RequestMapping("/user");
What if i want to declare another path in this same controller? For example im have #GetMapping("/info") and i will get info about user, but what if i want to declare on this same controller path localhost:8080/topic/blablabla? Is another solution than delete #RequestMapping from controller and make on every Get/PostMapping another path?
Defining a #RequestMapping at the controller level; it means narrowing it down to your criteria.
You can use the #RequestMapping annotation to map requests to controllers methods. It has various attributes to match by URL, HTTP method, request parameters, headers, and media types. You can use it at the class level to express shared mappings or at the method level to narrow down to a specific endpoint mapping. Read More
It is good you want to do, sometimes I need it too but as far as I research it is not supported now.

Using spring MVC to transform query parameters into path parameters

As part of SNS optimization we have to change our query parameters to path parameters, so:
/somesite?hl=ja
Has to become:
/somesite/hl/ja
How do we do that without adding a separate path to every single #RequestMapping method? It doesn't look like I can do it with an Interceptor. We are using Spring 4
You can try adding filter instead of interceptor to rewrite the request URI.
If you are free to use any third-party library, you can use this http://tuckey.org/urlrewrite/.
Or you can write your own impl in filter method.
Follow this question for implementation. servlet filter to rewrite URL

Packing multiple parameters into one inside Spring MVC Controller

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.

Read a form bean property to pass on to a forward

I am using struts 1.3. I have a an action I am reusing in 3 different cases. The form bean backing this has a property that has the complete path(passed from the jsp) to which the action should forward in case of success/failure(the path is different for each case depending on what the user is doing). How do I specify this for the input attribute of the action in the struts config to read from that form bean property.
What you can do is return a dynamic ActionForward from your Action class. This lets you use an ActionForward that isn't defined in yourstruts-config.xml
return new ActionForward(path_to_forward, redirect_true_or_false);
This doesn't help you for the input, which expects a JSP and not an ActionForward, but you should be able to do that in the ActionForm's validate() method. The first parameter passed into that method is an ActionMapping. On that object, you should be able to call setInput(String) with the path of your JSP. I have not tried this, but it looks like it should work.

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