2010-09-26 18 views
14

नियंत्रक में, मेरे पास यह कोड है, किसी भी तरह, मैं अनुरोध मैपिंग मान "खोज" प्राप्त करना चाहता हूं। यह कैसे संभव है?मैं नियंत्रक में requestmapping मान कैसे प्राप्त करूं?

@RequestMapping("/search/")  
public Map searchWithSearchTerm(@RequestParam("name") String name) {  
     // more code here  
} 
+0

आप अपने उपयोग के मामले पर विस्तार कर सकता है कृपया कर सकते हैं? मैं यह पता लगाने की कोशिश कर रहा हूं कि आप यहां क्या प्राप्त करने का प्रयास कर रहे हैं, क्योंकि खोज को लॉग करने या पूर्ण पथ का उपयोग करने के अलावा अन्य उपयोगी नहीं लगता है, इस मामले में आप अनुरोध से पथ प्राप्त कर सकते हैं क्योंकि अंकित ने – walnutmon

उत्तर

15

एक तरीका है इसे सर्वलेट पथ से प्राप्त करना।

@RequestMapping("/search/")  
public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {  
String mapping = request.getServletPath(); 
     // more code here  
} 
1
@RequestMapping("foo/bar/blub")  
public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) { 
    // delivers the path without context root 
    // mapping = "/foo/bar/blub" 
    String mapping = request.getPathInfo(); 
    // more code here 
} 
17

आप पैटर्न चाहते हैं, आप 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); 

} 
7

तरह

@Controller 
@RequestMapping(value = "/web/objet") 
public class TestController { 

    @RequestMapping(value = "/save") 
    public String save(...) { 
     .... 
    } 
} 

एक नियंत्रक प्राप्त करने से आपको प्रतिबिंब

का उपयोग कर नियंत्रक आधार requestMapping प्राप्त नहीं कर सकते
// Controller requestMapping 
String controllerMapping = this.getClass().getAnnotation(RequestMapping.class).value()[0]; 

या विधि requestMapping (एक विधि के अंदर से) प्रतिबिंब के साथ भी

//Method requestMapping 
String methodMapping = new Object(){}.getClass().getEnclosingMethod().getAnnotation(RequestMapping.class).value()[0]; 

जाहिर एकल मान requestMapping में एक साथ काम करता है।

उम्मीद है कि इससे मदद मिलती है।

+0

को निर्देश दिया था लॉजिक के साथ एक सार आधार नियंत्रक का उपयोग करते समय यह उपयोगी होता है जिसके लिए विरासत, कंक्रीट, नियंत्रक वर्ग –

+0

RequestMapping नहीं है? http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html – vzamanillo

0

स्प्रिंग 3.1 के लिए और आप ऊपर का उपयोग ServletUriComponentsBuilder

@RequestMapping("/search/")  
    public ResponseEntity<?> searchWithSearchTerm(@RequestParam("name") String name) { 
     UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest(); 
     System.out.println(builder.buildAndExpand().getPath()); 
     return new ResponseEntity<String>("OK", HttpStatus.OK); 
    } 
संबंधित मुद्दे