Announcement Announcement Module
Collapse
No announcement yet.
@RequestMapping and multiple directories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    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

    Comment


    • #3
      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, 12:04 PM. Reason: layout

      Comment


      • #4
        Please try

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


        liang xiao

        Comment


        • #5
          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

          Comment


          • #6
            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.

            Comment

            Working...
            X