Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 715
Leon Avatar asked Sep 03 '12 14:09

Leon


People also ask

How do you find the path variable in interceptor?

Add a Interceptor. Register the Interceptor. With this you will be able to read the @PathVariable by the name you have given to the pathvariable for all the request made.

What is path variable in Spring MVC?

@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map<String, String> then the map is populated with all path variable names and values.

How does interceptor work in Spring MVC?

Spring Handler Interceptor The HandlerInterceptor contains three main methods: prehandle() – called before the execution of the actual handler. postHandle() – called after the handler is executed. afterCompletion() – called after the complete request is finished and the view is generated.

What is difference between @PathParam and @PathVariable?

@PathParam: it is used to inject the value of named URI path parameters that were defined in @Path expression. @Pathvariable: This annotation is used to handle template variables in the request URI mapping ,and used them as method parameters.


1 Answers

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);  
like image 170
ashario Avatar answered Sep 18 '22 14:09

ashario