Using Spring, mapping to root in web.xml, static resources aren't found

你说的曾经没有我的故事 提交于 2019-11-26 23:37:45
axtavt

The problem is that requests for the static content go to the dispatcherServlet, because it's mapped as <url-pattern>/</url-pattern>. It's a very common problem in applications with "RESTful" URLs (that is, without any prefix in the DispatcherServlet mapping).

There are several possible ways to solve this problem:


Since Spring 3.x the preferred way to access static resources is to use <mvc:resources>: web.xml:

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>

Spring config:

<!-- Handles GET requests for /resources/** by efficiently serving static content 
    in the ${webappRoot}/resources dir --> 
<mvc:resources mapping="/resources/**" location="/resources/" /> 

See also MVC Simplifications in Spring 3


1. Use URL rewrite filter
See mvc-basic example here

2. Set a prefix for the default servlet:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

That is, request for /static/images/image.png will return the file named /images/image.png However, this way is incompatible across different servlet containers (doesn't work in Jetty), see workarounds here

3. Set static content extensions for the default servlet:

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

4. Do not use RESTful URLs, use URLs with prefix:

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/app</url-pattern> 
</servlet-mapping>

5. Do not use RESTful URLs, use URLs with extension:

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>

Did any one consider using this:

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

Here is the latest spring docs on it: http://static.springsource.org/spring/docs/3.1.2.RELEASE/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-default-servlet-handler

ngeek

As an alternative to the proposed solution number (2, default servlet which behaves differently from servlet container to servlet container), I would recommend taking a look into the Resource Servlet (org.springframework.js.resource.ResourceServlet) from the Spring Webflow project.

For more details please have a look at How to handle static content in Spring MVC?

I have the same problem but instead of using spring, I do mysefl a small filter that redirect root to my start page like that:

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String pageName = req.getServletPath();

    if(pageName.equals("/")) {
        res.sendRedirect( req.getContextPath() + "/start" );
    } else {
        chain.doFilter(request, response);
    }

It's maybe a trick but it look like working fine with a small code. Go here for more filter information http://www.oracle.com/technetwork/java/filters-137243.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!