5

I have the following mapping for a controller handler method:

@RequestMapping(value = "login.html")
public String doLogin(Model model) {
    return "login";
}

And I have the following configuration for static *.html resource:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
     registry.addResourceHandler("*.html").addResourceLocations(("/static/"));
}

So suppose I am visiting login.html, which one will take the precedence? Any official document about the by-design behavior?

ADD 1 Some background

I want to use pure HTML+JS for the view of my application. Since I don't know how to return an HTML from a controller method (someone said it is not possible), I decided to serve the static HTML with static resource handler. But it seems I still have to configure a handler method mapped to the root path / for my web application. Although I have already placed an index.html under the /static/ path. i.e., I must have this:

@RequestMapping(value = "/")
public String welcome(Model model) {
    return "redirect:index.html"; //must prefix with "redirect:"
}

Otherwise, when I hit http://mysite/, I will get a 404 error.

ADD 2 -- some experiment result

It seems the precedence is like this:

request comes -> Controller request mapping -> If no valid mapping in controller, check static resource handler -> if no valid static resource found, 404, Oops...

ADD 3 - 3 possible options to serve static resource.

I moved the question to here:

Static resource serving in SpringMVC

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482

1 Answers1

2

The WebMvcConfigurationSupport Javadoc describes the HandlerMapping beans registered by default (i.e. by @EnableWebMvc) and their order of precedence.

When mapping URL paths, the order is:

  1. Annotated controller methods
  2. Directly to view names
  3. Controller bean names
  4. Serve static resource requests
  5. Forward requests to the default servlet

Even if you're building a "pure HTML+JS" application, you might want to turn that index.html into a template anyway and leverage some Spring MVC features, such as resource handling and cache busting for your static resources. Take a look at this Devoxx talk.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Thanks for the answer. I am new to Spring and I prefer not to jump to Spring Boot too soon. Could you elaborate a bit more about `turning index.html into a template`? – smwikipedia Dec 05 '15 at 11:31
  • The Spring team recommends Spring Boot for both experienced developers and newcomers http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#overview-getting-started-with-spring . As for your other questions, I think they're orthogonal to your first question and belong to a separate SO thread. Remember, this should be also useful to others and not overwhelm readers with content and multiple questions. – Brian Clozel Dec 05 '15 at 12:47
  • Got it. I will start another thread for my `ADD 3` after I prepare my words. – smwikipedia Dec 05 '15 at 12:54
  • @BrianClozel Is it possible to do/fix the following: `@GetMapping("/x/**") public String fw() { return "forward:/x/index.html"; }` <-- this of course creates an endless loop - is it possible to prevent this? Motivation: Javascript-Frontend with router in history mode must forward all URLs /x/** to javascript router. If the code above can be fixed, all javascript assets can be placed e.g. in `/src/main/resources/static/x/`. Of course different names can be used e.g. `x` and `y` ;-), but this is not as nice and runs into problems when running a js dev server. Thanks in advance! – bgraves Apr 25 '23 at 13:14