Spring mvc @PathVariable

后端 未结 8 794
轮回少年
轮回少年 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 12:05

    Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods.

    @RequestMapping(value = "/download/{documentId}", method = RequestMethod.GET)
    public ModelAndView download(@PathVariable int documentId) {
        ModelAndView mav = new ModelAndView();
        Document document =  documentService.fileDownload(documentId);
    
        mav.addObject("downloadDocument", document);
        mav.setViewName("download");
    
        return mav;
    }
    
    0 讨论(0)
  • 2020-11-27 12:08

    have a look at the below code snippet.

    @RequestMapping(value = "edit.htm", method = RequestMethod.GET) 
        public ModelAndView edit(@RequestParam("id") String id) throws Exception {
            ModelMap modelMap = new ModelMap();
            modelMap.addAttribute("user", userinfoDao.findById(id));
            return new ModelAndView("edit", modelMap);      
        }
    

    If you want the complete project to see how it works then download it from below link:-

    UserInfo Project on GitLab

    0 讨论(0)
提交回复
热议问题