How to handle static content in Spring MVC?

前端 未结 23 2484
春和景丽
春和景丽 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 03:48

    I know there are a few configurations to use the static contents, but my solution is that I just create a bulk web-application folder within your tomcat. This "bulk webapp" is only serving all the static-contents without serving apps. This is pain-free and easy solution for serving static contents to your actual spring webapp.

    For example, I'm using two webapp folders on my tomcat.

    1. springapp: it is running only spring web application without static-contents like imgs, js, or css. (dedicated for spring apps.)
    2. resources: it is serving only the static contents without JSP, servlet, or any sort of java web application. (dedicated for static-contents)

    If I want to use javascript, I simply add the URI for my javascript file.

    EX> /resources/path/to/js/myjavascript.js

    For static images, I'm using the same method.

    EX> /resources/path/to/img/myimg.jpg

    Last, I put "security-constraint" on my tomcat to block the access to actual directory. I put "nobody" user-roll to the constraint so that the page generates "403 forbidden error" when people tried to access the static-contents path.

    So far it works very well for me. I also noticed that many popular websites like Amazon, Twitter, and Facebook they are using different URI for serving static-contents. To find out this, just right click on any static content and check their URI.

提交回复
热议问题