How to handle static content in Spring MVC?

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

    This problem is solved in spring 3.0.4.RELEASE where you can use <mvc:resources mapping="..." location="..."/> configuration element in your spring dispatcher configuration file.

    Check Spring Documentation

    0 讨论(0)
  • 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:

    <servlet>
        <servlet-name>mainDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>mainDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    

    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:

    <dependency>
        <groupId>org.springframework.webflow</groupId>
        <artifactId>org.springframework.js</artifactId>
        <version>2.0.8.RELEASE</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-22 04:09

    If I understand your issue correctly, I think I have found a solution to your problem:

    I had the same issue where raw output was shown with no css styles, javascripts or jquery files found.

    I just added mappings to the "default" servlet. The following was added to the web.xml file:

     <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.css</url-pattern>
     </servlet-mapping>
    
     <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
     </servlet-mapping>
    

    This should filter out the javascript and css file requests from the DispatcherRequest object.

    Again, not sure if this is what you are after, but it worked for me. I think "default" is the name of the default servlet within JBoss. Not too sure what it is for other servers.

    0 讨论(0)
  • 2020-11-22 04:11

    After encountering and going through the same decision making process described here, I decided to go with the ResourceServlet proposal which works out quite nicely.

    Note that you get more information on how to use webflow in your maven build process here: http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch01s05.html

    If you use the standard Maven central repository the artifact is (in opposite to the above referred springsource bundle):

    <dependency>
        <groupId>org.springframework.webflow</groupId>
        <artifactId>spring-js</artifactId>
        <version>2.0.9.RELEASE</version>
    </dependency> 
    
    0 讨论(0)
  • 2020-11-22 04:12

    In Spring 3.0.x add the following to your servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;)

    That works for me

    <mvc:default-servlet-handler/>
    

    Regards

    Ayub Malik

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