Results 1 to 3 of 3

Thread: Access to wildcard @RequestMapping path within controller

  1. #1
    Join Date
    Aug 2004
    Location
    Los Angeles, USA
    Posts
    62

    Default Access to wildcard @RequestMapping path within controller

    Hi,

    I need to know the value of the wildcard match for a RequestMapping within a controller's method. I realize a PathVariable can be used to access part of the path but I have not found a way to access full wildcard matches.

    For example:
    Code:
    @RequestMapping(value = "/cms/*", method = RequestMethod.GET)
    public void renderContent() {
     // I want to know what came behind cms. I.e. if the URI is /cms/article/something.html I want to get access to "/article/something.html. If the URI is /cms/image/something.png I need to know the "/image/something.png
    }
    I could inject the HttpServletRequest but that won't work when the controller is called from within a jsp:include or tiles. Hence, I need a reliable way to access the wildcard value.

    The use case for this is a generic content controller that takes whatever URI that comes after /content/ and matches that to a content file that can be located at any folder depth with any file extension.

    Thanks!

    Thomas

  2. #2
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Check out URI Templates in the documentation. It should give you what you're after.

  3. #3
    Join Date
    Aug 2004
    Location
    Los Angeles, USA
    Posts
    62

    Default

    None of that works. I can find a way to get to nested paths in the @PathVariable.

    You can do something like this:

    Code:
    @RequestMapping("/content")
    public class CmsContentController {
    
    @RequestMapping(value = "{cms:.*}", method = RequestMethod.GET)
    	public void renderContent(@PathVariable String cms) {
    
    }
    When you request /content/something.html, cms will resolve to "something.html"

    That's great, but it doesn't work for nested paths.

    it looks like you can only get to a single folder in a path but never an entire wildcard match for a subdirectory tree (i.e. **/*.*)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •