How to handle static content in Spring MVC?

前端 未结 23 2457
春和景丽
春和景丽 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:06

    My own experience with this problem is as follows. Most Spring-related web pages and books seem to suggest that the most appropriate syntax is the following.

        
    

    The above syntax suggests that you can place your static resources (CSS, JavaScript, images) in a folder named "resources" in the root of your application, i.e. /webapp/resources/.

    However, in my experience (I am using Eclipse and the Tomcat plugin), the only approach that works is if you place your resources folder inside WEB_INF (or META-INF). So, the syntax I recommend is the following.

        
    

    In your JSP (or similar) , reference the resource as follows.

    
    

    Needless to mention, the entire question only arose because I wanted my Spring dispatcher servlet (front controller) to intercept everything, everything dynamic, that is. So I have the following in my web.xml.

    
        front-controller
        
                    org.springframework.web.servlet.DispatcherServlet
        
        1
        
    
    
    
        front-controller
        /
    
    

    Finally, since I'm using current best practices, I have the following in my front controller servlet xml (see above).

    
    

    And I have the following in my actual controller implementation, to ensure that I have a default method to handle all incoming requests.

    @RequestMapping("/")
    

    I hope this helps.

提交回复
热议问题