Mapping same URL to different controllers in spring based on query parameter

≯℡__Kan透↙ 提交于 2020-01-03 12:59:10

问题


I'm using spring annotation based controller. I want my URL /user/messages to map to some controller a if query parameter tag is present otherwise to some different controller b. This is required because when parameter tag is present then some more parameters can be present along with that which i want to handle in different controller to keep the implementation clean.Is there any way to do this in spring. Also is there any other elegant solution to this problem ?


回答1:


If you want to go the Spring route, you can checkout the HandlerInterceptor mentioned here. The Interceptor can take a look at your query param and redirect the link to something else that can be caught by another SimpleUrlMapper.

The other way is to send it to a single controller and let the controller forward to another action if the query parameter is "b".




回答2:


You can use the params attribute of the @RequestMapping annotation to select an controller method depending on Http parameters.

See this example:

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

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

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



来源:https://stackoverflow.com/questions/6448895/mapping-same-url-to-different-controllers-in-spring-based-on-query-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!