Spring mvc 3 : How to get path variable in an interceptor?

北城余情 提交于 2019-12-03 09:21:00

问题


In Spring MVC controller, I can get path variable using @PathVariable to get the value of a variable defined in @RequestMapping. How can I get the value of the variable in an interceptor?

Thank you very much!


回答1:


The thread linked to by Pao worked a treat for me

In the preHandle() method you can extract the various PathVariables by running the following code

Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); 



回答2:


There is a thread in the Spring forums, where someone says, there is no "easy way", so i suppose you would have to parse the URL to get it.




回答3:


Almost 1 year too late, but:

         String[] requestMappingParams = ((HandlerMethod)handler).getMethodAnnotation(RequestMapping.class).params()

         for (String value : requestMappingParams) {...

should help



来源:https://stackoverflow.com/questions/12249721/spring-mvc-3-how-to-get-path-variable-in-an-interceptor

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