Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get the requestmapping value in the controller?

Tags:

spring-mvc

In the controller , i have this code, somehow, i want to get the request Mapping value "search". How is it possible ?

 @RequestMapping("/search/")       public Map searchWithSearchTerm(@RequestParam("name") String name) {             // more code here       } 
like image 730
storm_buster Avatar asked Sep 26 '10 04:09

storm_buster


2 Answers

If you want the pattern, you can try HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE:

@RequestMapping({"/search/{subpath}/other", "/find/other/{subpath}"}) public Map searchWithSearchTerm(@PathVariable("subpath") String subpath,                                              @RequestParam("name") String name) {      String pattern = (String) request.getAttribute(                                  HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);     // pattern will be either "/search/{subpath}/other" or     // "/find/other/{subpath}", depending on the url requested     System.out.println("Pattern matched: "+pattern);  } 
like image 176
acdcjunior Avatar answered Sep 18 '22 18:09

acdcjunior


It seems you are looking for the path that this request has matched, then you can directly get it from servlet path

@RequestMapping("/search/")       public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {     String path = request.getServletPath();         // more code here       } 
like image 36
Ankit Bansal Avatar answered Sep 20 '22 18:09

Ankit Bansal