Mapping the same URL for different controllers in spring based on request parameter

I am using an annotation based w760> controller. I want my url / user / posts to display on some kind of controller. If the request parameter tag is present otherwise for some other controller b. This is necessary because when a parameter tag is present, several more parameters may be present along with what I want to process in another controller to ensure a clean implementation. Is there any way to do this in spring. Is there also another elegant solution to this problem?

+5
source share
2 answers

If you want to take the Spring route, you can check here HandlerInterceptor here . An interceptor can take a look at your request parameter and redirect the link to something else that could be detected by another SimpleUrlMapper.

Another way is to send it to one controller and allow the controller to go to another action if the request parameter is "b".

+3
source

You can use the paramsannotation attribute @RequestMappingto select the controller method depending on the Http parameters.

See this example:

@RequestMapping(params = "form", method = RequestMethod.GET)
public ModelAndView createForm() {
    ...
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView list() {
    ...
}

This is a REST style, for example Spring ROO uses: if the request contains a parameter forms, then a handler is used createForm, if a method is not used list.

+5
source

All Articles