Spring mvc @PathVariable

后端 未结 8 846
轮回少年
轮回少年 2020-11-27 11:41

Can you give me a brief explanation and a sample in using @PathVariable in spring mvc? Please include on how you type the url?
I\'m struggling in getting th

8条回答
  •  借酒劲吻你
    2020-11-27 11:54

    It is one of the annotation used to map/handle dynamic URIs. You can even specify a regular expression for URI dynamic parameter to accept only specific type of input.

    For example, if the URL to retrieve a book using a unique number would be:

    URL:http://localhost:8080/book/9783827319333
    

    The number denoted at the last of the URL can be fetched using @PathVariable as shown:

    @RequestMapping(value="/book/{ISBN}", method= RequestMethod.GET)
    
    public String showBookDetails(@PathVariable("ISBN") String id,
    
    Model model){
    
    model.addAttribute("ISBN", id);
    
    return "bookDetails";
    
    }
    

    In short it is just another was to extract data from HTTP requests in Spring.

提交回复
热议问题