Match for root url and serving of static resources

前端 未结 1 878
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 16:04

My problem is how to configure a Spring MVC application to allow at the same time

  • application should serve static resources (css, js, images ...)
  • the
相关标签:
1条回答
  • 2020-11-28 16:51

    I will begin with a preliminary remark about how DefaultServlet works. According to Servlet 3.0 specifications, containers generally provide a default servlet, that has lowest priority and serves static context. The mapping / is the implicit mapping for this default servlet.

    Now for the solutions :

    Map spring controllers to a sub-hierarchy

    That is the easiest solution : you map Spring DispatcherServlet to /pages, or to /pages and /api for example. The default servlet will then serve all other URLs (including root). To serve root controller, you can map a controller to /home (for example) and have a /index.jsp containing <jsp:forward page="/home"/> - this is a method of current use in other frameworks using extension mapping such as Struts (*.do for old Struts1).

    Drawbacks : having url stating with /pages is not very nice.

    Map resources to a sub-hierarchy

    This solution is highly used in the referenced pages. Spring DispatcherServlet is mapped to /* and so gets all the requests (unless a more specific mapping exists). To serve static resources, you just declare a ResourceHttpRequestHandler, using in XML :

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

    or in java configuration :

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
        }
    }
    

    This works very fine, and you can map a Spring controller to / directly.

    Drawbacks : you cannot serve static resources that would be directly under root context.

    Map DispatcherServlet as the default servlet

    Mapping Spring DispatcherServlet to / is in fact replacing the default servlet from the container to process all not already processed URLs. With this mapping, Spring can fallback to the original default servlet for URLs not mapped to controllers. For that to work, you have to configure a DefaultServletHttpRequestHandler with a URL mapping of "/**" and the lowest priority. You do it using XML :

    <mvc:default-servlet-handler/>
    

    or in java configuration :

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
    }
    

    That way, DispatcherServlet normally calls all controllers, and have the original default servlet to serve static (not mapped) resources. Unfortunately, this does not work for the root URL, and you must use the <jsp:forward page="..."/> trick like for first solution.

    Drawbacks :

    • cannot directly map root URL and need the index.jsp <jsp:forward page="..."/> trick
    • As Spring as replaced original container default servlet, it must call it by name. It works for common containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere), or you can also give the name for default servlet as an attribute in XML config (<mvc:default-servlet-handler default-servlet-name="customDefaultServlet"/>) or as a parameter if java configuration: configurer.enable("customDefaultServlet");

    References : Spring Reference Manual / Web MVC framework / Serving of Resources

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