2

I am using SpringMVC and trying to use pure HTML + JS as the view. I noticed that there are potentially 3 possible places that could handle a static resource like login.html. They are:

  1. The controller handler method which is mapped to a seemingly static resource URL.

    @RequestMapping(value = "login.html")
    public String doLogin(Model model) {
        return "login";
    }
    
  2. The addResourceHandlers which designates the static resource type and location:

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("*.html").addResourceLocations(("/static/"));
    }
    
  3. And Tomcat's DefaultServlet, which by definition, is meant to serve static resources. And can be enabled in SpringMVC as below:

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        // TODO Auto-generated method stub
        configurer.enable();
    }
    

My questions are:

  • How does a servlet container like Tomcat determine a resource is static or not? Is it based on some well-known file name extension?
  • What's the precedence of the 3 options above?
  • What's the relationship between the Spring configuration method addResourceHandler() and the Tomcat DefaultServlet? My guess is addResourceHandler() tells DefaultServlet where to find certain type of static resources.

Some related links:

21.16.9 Serving of Resources

Resource served by SpringMVC's ResourceHttpRequestHandler.

21.16.10 Falling Back On the "Default" Servlet To Serve Resources

Resource served by servlet container's DefaultServet via SpringMVC's DefaultServletHttpRequestHandler

matsev
  • 32,104
  • 16
  • 121
  • 156
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 1) is technically answered in http://stackoverflow.com/q/4140448. 2) is not a question for stack overflow. 3) it has nothing to do with specifically Tomcat's `DefaultServlet`. Every container has one. It's just the "fallback" servlet in case no servlet is invoked. In the future, it's more useful for you and us if you ask one question per Question. – BalusC Dec 05 '15 at 15:36
  • answer to your first question will be; servlet container determines if resource is static based on the url mapping.., say anything that is under /resource/** is static etc.. – Jos Dec 05 '15 at 15:36
  • 1
    @redflar3: nope, servlet container does't determine that. Spring MVC determines that. You're confusing Spring MVC with Servlet. – BalusC Dec 05 '15 at 22:41

0 Answers0