4

我正在使用 Spring 3.1 MVC,并且在其中一个请求中我得到了 URL 参数。例如http:/myapp/controllername?url=someurl ,我需要在我的控制器方法中找出我的应用程序中是否配置了某些 URL。

我尝试使用RequestMappingHandlerMapping实例来获取

Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();

但是我有一个字符串,它是 URL,我需要创建一个RequestMappingInfo对象来在这个地图中查找,它没有任何基于 URL 的构造函数。

如何使用控制器中的代码查找 Spring mvc 3.1 中是否存在 URL 映射?

4

1 回答 1

4

您可以使用 requestMappingInfo.getPatternsCondition() 来检查映射路径;它有 toString 方法供您与您的 url 字符串进行比较。

Set<RequestMappingInfo> rmSet = handlerMapping.getHandlerMethods().keySet();
for (RequestMappingInfo rm : rmSet) {

    if("[YourURLPath]".equals(rm.getPatternsCondition().toString())) {
        // URL mapping matched 
    }
}

示例:对于 url http://mydomain/test/abc,上述条件应为

if("[/test/abc]".equals(rm.getPatternsCondition().toString()))

我相信您已经在控制器中自动装配了 handlerMapping 对象,如下所示

@Autowired
private RequestMappingHandlerMapping handlerMapping;
于 2012-11-28T08:42:49.577 回答