Announcement Announcement Module
Collapse
No announcement yet.
Returning static files with Spring MVC and Tomcat Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning static files with Spring MVC and Tomcat

    Hi -

    [ This is a question about the collaboration between Spring and Tomcat configurations for serving CERTAIN urls as just static file resources and all others via Spring Controllers ]

    I'm a relative newcomer to Spring, but have got it doing more or less what I want for my starter web application with Velocity views, which are mapped from multiple styles of url (including several extensions AND directory listings). I've done this by making web.xml map everything (<url-pattern>/</url-pattern> in my web.xml) to my spring dispatcher servlet then sorting out which Controller will respond with a HandlerMapping in my spring config.

    However, now I want certain files (*.css, *.gif, *.png, etc) to be just served up.

    What's the best way of doing this? The web.xml's <url-pattern> is pretty limited, and there seems to be no Controller which will just feed back the file (not the view) as is (though FileNameViewController is pretty close). Do I have to make my own controller that does this, or am I missing something.

    Many thanks

    Tim

  • #2
    I've done this by making web.xml map everything (<url-pattern>/</url-pattern> in my web.xml) to my spring dispatcher
    This is probably not a good idea. Just map your URLs that need a controller to the dispatcher, and let Tomcat serve the static files up (simpler and less overhead).

    Comment


    • #3
      Just map your URLs that need a controller to the dispatcher
      That sounds good, but there isn't a way (as far as I've been able to tell) to map "directory" URLS (no extension) only to the dispatcher using web.xml's url-pattern -- you have to map all.

      Comment


      • #4
        Tim,

        I normally set up a mapping like the following for the spring dispatcher servlet:

        Code:
        <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
        I have no files with an .htm extension actually in the web app. This is a dummy extension that I use to invoke the spring mvc. I make sure the request urls have this extension in my pages and that they are correctly in the mappings in the spring context file.

        I also sometimes have another servlet (maybe the axis servlet) that I need to map. So I normally have something like:

        Code:
        <servlet-mapping>
        <servlet-name>axis</servlet-name>
        <url-pattern>/axis/*</url-pattern>
        </servlet-mapping>
        I just need to ensure that I tack on the /axis/ after the web app context name to make sure the mapping is hit.

        Based on these mappings, all the other files requested (*.html, *.css, etc.) will be servered by the web server.

        Hope this helps.

        -Russ.

        Comment

        Working...
        X