Results 1 to 6 of 6

Thread: @RequestMapping and multiple directories

  1. #1
    Join Date
    Jun 2006
    Posts
    7

    Default @RequestMapping and multiple directories

    I'm stuck trying to get a @RequestMapping into a @PathVariable with multiple directories in it.

    The url would be something like:
    http://mydomain.com/servlet/image/subdir/another_subdir/image1.gif

    The part I would like to match is in bold. It should be able to anything after "image/". That way I can retrieve this image form my filesystem and serve it to the user. (Please not that directly exposing the images is not an option, as they are in WEB-INF.)
    I could work around it using the urlrewrite filter, but would prefer to do it within Spring.

    I have tried RequestMappings with a regex:

    Code:
    @RequestMapping(value = "/document/image/{filename:.*}", method = RequestMethod.GET)
    Unfortunately this doesn't work for subdirectories, only the top-level directory works.

    Any ideas?

  2. #2

    Default

    Try looking at implementing a RequestToViewNameTranslator

    It'll automatically translate to a view name based on the incoming request. That way you don't need to have contoller method just for forwarding to the view.
    An out of the box simple implementation of the same is the DefaultRequestToViewNameTranslator. Refer to the javadocsfor details
    -Amit

  3. #3
    Join Date
    Jun 2006
    Posts
    7

    Default annotation way?

    Thanks amit.kapps. Maybe I just don't really get what you are saying, but I'd really like to do this using annotations. I am using these annotated methods all over my controller files, which are annotated with @Controller.
    By the way, my controller is not just forwarding to the view, it handles writing the file to the OutputStream and returns null/void. So I really do need the controller.

    I would really prefer not to write a RequestToViewNameTranslator, because that would scatter my configuration over multiple methods.

    Let me restate my original question: How do I map an entire subdirectory to a certain controller with an annotation?
    Last edited by springbee; Mar 18th, 2010 at 12:04 PM. Reason: layout

  4. #4
    Join Date
    Mar 2010
    Posts
    11

    Default

    Please try

    @RequestMapping(value = "/document/image/{filename}", method = RequestMethod.GET)
    public String function(@PathVariable("filename")String filename);


    liang xiao

  5. #5
    Join Date
    Jun 2006
    Posts
    7

    Default

    That doesn't work either. It maps the top level, but leaves out the extension (which it normally uses for detecting the content type to return).
    It was also the first thing I tried. Then I found out that you need the regular expression {filename:.*} to keep the extension. But I can't seem to map to entire subdirectories.

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webapp-rest/rest/test/document/image/test/ballmer.png] in DispatcherServlet

  6. #6

    Default

    Do something like-
    Code:
    @RequestMapping(value = "/document/image/**", method =  RequestMethod.GET)
    And then in your method, extract/substring the the request path from HttpServlet object yourself and return the view path according to your logic.
    -Amit

Tags for this Thread

Posting Permissions

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