How to handle static content in Spring MVC?

前端 未结 23 2502
春和景丽
春和景丽 2020-11-22 03:28

I am developing a webapp using Spring MVC 3 and have the DispatcherServlet catching all requests to \'/\' like so (web.xml):

  
          


        
23条回答
  •  被撕碎了的回忆
    2020-11-22 04:07

    I've just been grappling with this issue in Spring MVC 3.0 and I initially went with the UrlRewriteFilter option. However I was not happy with this solution as it "didn't feel right" (I'm not the only one - see the link above to the Spring Forums where the word "hack" appears a few times).

    So I came up with a similar solution to "Unknown (Google)" above but borrowed the idea of having all static content served from /static/ (taken from the Spring Roo version of the Pet Store app). The "default" servlet did not work for me but the Spring Webflow ResourceServlet did (also taken from Spring Roo generated app).

    Web.xml:

    
        mainDispatcher
        org.springframework.web.servlet.DispatcherServlet
        2
    
    
    
        Resource Servlet
        org.springframework.js.resource.ResourceServlet
        1
    
    
    
        mainDispatcher
        /
    
    
    
        Resource Servlet
        /static/*
    
    

    The only change I made to JSPs was to add the /static/ path to URLs for CSS, JS and images. E.g. "${pageContext.request.contextPath}/static/css/screen.css".

    for Maven users the dependency for "org.springframework.js.resource.ResourceServlet" is:

    
        org.springframework.webflow
        org.springframework.js
        2.0.8.RELEASE
    
    

提交回复
热议问题