@RequestMapping regular expression

后端 未结 1 600
半阙折子戏
半阙折子戏 2021-01-14 13:25

I\'m trying to make value atribute for spring @RequestMapping annotation to map url like this

/educationDistrict/308/action/resetAddressesForYear/1


        
相关标签:
1条回答
  • 2021-01-14 13:44

    Add /** at the end of the URL mapping in @RequestMapping. And you can retrieve last part of the URL as below:

    @RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}/**", method = RequestMethod.GET)
    public ModelAndView welcome(@PathVariable("methodName") String name, HttpServletRequest request) {
    
        String mvcPath = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        int index = StringUtils.lastIndexOf(mvcPath, "/");
    
        System.out.println("Method name - " + name);        
        System.out.println("Rest of the URL - " + mvcPath.substring(index+1));
    
        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        model.addObject("name", mvcPath);
    
        return model;
    }
    

    Note: I have used StringUtils Apache Commons to find the last index of /.

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