2011-09-29 11 views
11

का उपयोग करते समय अनुरोधित मान (यूआरएल) प्राप्त करें जब मैं @RequestMapping (जैसे Multiple Spring @RequestMapping annotations) पर एकाधिक मान मैप करता हूं, तो क्या मुझे अनुरोधित मान (यूआरएल) मिल सकता है?@RequestMapping एनोटेशन

इस तरह

:

@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET) 
public String getCenter(Model model) throws Exception {  
    String requestedValue = getRequestedValue(); // I want this. 

    // I want to do something like this with requested value. 
    String result; 
    if (requestedValue.equals("center") 
     result = "center"; 
    else if (requestedValue.equals("left") 
     result = "left"; 
    return result; 
} 

उत्तर

4

वसंत 3.1.0 से, आप URI Template Patterns with Regular Expressions का उपयोग कर सकते हैं। सर्वश्रेष्ठ उत्तर @Hugh_Lee को

@RequestMapping(value={"/{path:[a-z-]+}"}, method=RequestMethod.GET) 
public String getCenter(@PathVariable String path) throws Exception {    
    // "path" is what I want 
} 
1

उपयोग RequestParam एनोटेशन। आप अपनी विधि में HttpServletRequest प्रकार का पैरामीटर भी जोड़ सकते हैं और फिर उस से पैरामीटर प्राप्त कर सकते हैं।

+0

उपयोग कर सकते हैं यह तेजी कि Sangdol अलग यूआरएल, नहीं http मूल्यों मतलब है। (तो उसका शब्द "मूल्य" थोड़ा उलझन में है) – Ralph

17

आप हैंडलर विधि का एक पैरामीटर के रूप में अनुरोध (HttpServletRequest) ही हो सकता है। तो आप "मूल्य" प्राप्त करने के लिए अनुरोध url का निरीक्षण कर सकते हैं।

@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET) 
public String getCenter(Model model, HttpServletRequest request) throws Exception {    
    String whatYouCallValue = request.getServletPath(); 
    .... 

Btw: जब मैं तुम rigth समझते हैं, आप अलग अलग यूआरएल, नहीं अलग-अलग मान करना चाहते हैं।

+0

धन्यवाद यह काम करता है। मैंने इसे मूल्य कहा क्योंकि संपत्ति का नाम मूल्य है, लेकिन अब मुझे लगता है कि यह भ्रमित हो सकता है। मैं कुछ सही कर दूंगा। –

+0

मैंने अपना जवाब स्वीकार कर लिया, क्योंकि यह क्लीनर दिखता है। –

0

अलावा: इस विधि सभी मैप नहीं किया गया अनुरोध के लिए काम करेंगे। यदि आप केवल इस विधि का उपयोग केवल दो (या कई) मामलों के लिए करना चाहते हैं, उदा। "/ केंद्र" और "/ बाएं", आप निम्न कार्य कर सकते हैं। "सेंटर" को "positionCenter", "left" से "positionLeft" (या एक और सामान्य शब्द जोड़ें) का नाम बदलें। तो कोड इस तरह होगा:

@RequestMapping(value={"/{path:position+[A-Za-z-]+}"}, method=RequestMethod.GET) 
public String getCenter(@PathVariable String path) throws Exception {    
    // "path" is what I want 
} 
0

बाद regex अपने विधि यूआरएल /center और /left के लिए ही निष्पादित किया जाना है कर देगा। और आप @PathVariable एनोटेशन के साथ मान प्राप्त कर सकते हैं।

@GetMapping("/{path:^center$|^left$}") 
public ResponseEntity<?> whatIsThePath(@PathVariable String path){ 
    // path is either "center" or "left" 
} 
1

वसंत 3.1.0 से, आप ServletUriComponentsBuilder

@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET) 
    public String getCenter(Model model) throws Exception {  
     UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest(); 
     String requestedValue = builder.buildAndExpand().getPath(); // I want this. 
     System.out.println(requestedValue); 
     // I want to do something like this with requested value. 
     String result="fail"; 
     if (requestedValue.equals("center")) 
      result = "center"; 
     else if (requestedValue.equals("left")) 
      result = "left"; 
     return result; 
    } 
संबंधित मुद्दे