在弹簧中基于查询参数将相同的URL映射到不同的控制器 [英] Mapping same URL to different controllers in spring based on query parameter

查看:75
本文介绍了在弹簧中基于查询参数将相同的URL映射到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于spring注释的控制器.我希望我的URL/user/messages映射到某个控制器a(如果存在查询参数标记),否则映射到某些其他控制器b.这是必需的,因为当存在参数标记时,可以存在更多的参数以及我想在其他控制器中处理的参数,以保持实现的清洁度.春季有什么方法可以做到这一点.还有其他优雅的解决方案可以解决这个问题吗?

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 ?

推荐答案

如果要走Spring路线,可以签出提到的HandlerInterceptor

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.

另一种方法是将其发送到单个控制器,如果查询参数为"b",则让控制器转发到另一个操作.

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

这篇关于在弹簧中基于查询参数将相同的URL映射到不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆