How to handle static content in Spring MVC?

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

    There's another stack overflow post that has an excellent solution.

    It doesn't seem to be Tomcat specific, is simple, and works great. I've tried a couple of the solutions in this post with spring mvc 3.1 but then had problems getting my dynamic content served.

    In brief, it says add a servlet mapping like this:

    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/images/*</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
  • 2020-11-22 03:45

    From Spring 3, all the resources needs to mapped in a different way. You need to use the tag to specify the location of the resources.

    Example :

    <mvc:resources mapping="/resources/**" location="/resources/" />
    

    By doing this way, you are directing the dispatcher servlet to look into the directory resources to look for the static content.

    0 讨论(0)
  • 2020-11-22 03:46

    The Problem is with URLPattern

    Change your URL pattern on your servlet mapping from "/" to "/*"

    0 讨论(0)
  • 2020-11-22 03:47

    I got the same problem and found Joris's answer very helpful. But additionally I need to add

    <mvc:annotation-driven /> 
    

    to the servlet config file. Without that resource mapping will not work and all handlers will stop working. Hope this will help someone.

    0 讨论(0)
  • 2020-11-22 03:48

    I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite.xml) to solve the problem

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
        <urlrewrite default-match-type="wildcard">
         <rule>
          <from>/</from>
          <to>/app/welcome</to>
         </rule>
         <rule>
          <from>/scripts/**</from>
          <to>/scripts/$1</to>
         </rule>
         <rule>
          <from>/styles/**</from>
          <to>/styles/$1</to>
         </rule>
         <rule>
          <from>/images/**</from>
          <to>/images/$1</to>
         </rule>
         <rule>
          <from>/**</from>
          <to>/app/$1</to>
         </rule>
         <outbound-rule>
          <from>/app/**</from>
          <to>/$1</to>
         </outbound-rule> 
        </urlrewrite>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题