Spring (and also Spring Boot) provides great capability how static resources can be handling.
From Spring Boot point of view this functionality is preconfigured by org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
. In org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#addViewControllers
there is resolving of Welcome page (index.html) from static assets and if found, auto-redirection via forward:index.html
for _/_ path is registered.
Most of cases can use static resource handling in scenario from root path of application but why not in different? For example the same URL as for static resources? Actually heap is to use frontend plugin to generate whole SPA (Angular, React and so on) which can be hosted for example at /root/path/spa(/index.html)
. I think that for many application welcome page is not at _/_ path but can be for example /root/path/spa/
.
Does it make sense to register custom URL path for welcome view?
spring.mvc.welcome-page-path=/ # Path pattern used for static resources.
@Override
public void addViewControllers(ViewControllerRegistry registry) {
Resource page = this.resourceProperties.getWelcomePage();
if (page != null) {
logger.info("Adding welcome page ({}): {}", page, this.mvcProperties.getWelcomePagePath());
registry.addViewController(this.mvcProperties.getWelcomePagePath()).setViewName("forward:index.html");
}
}
Actually when someone needs to define custom welcome page it is necessary to register new view controller to ViewControllerRegistry
.
I'm afraid I'm not totally following the description. Are you saying that it's common for people to want to setup mappings so that if someone types /root/path/spa/
they get a forward to /root/path/spa/index.html
?
In other words, an easier way to do this:
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/root/path/spa/").setViewName("forward:index.html");
}
}
I am talking about default behaviour of current solution. Yes, I know how I can do it with custom code (check this one).
For example, I have static assets and configure spring boot following:
spring.mvc.static-path-pattern=/console/v2/**
spring.resources.static-locations=classpath:/META-INF/resources/console/
but I would like to register _index.html_ as welcome page under _/console/v2/_, but actually the welcome page is registered under root, _/_. We use Spring Boot 1.3.8, where welcome page is configured:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
Resource page = this.resourceProperties.getWelcomePage();
if (page != null) {
logger.info("Adding welcome page: " + page);
registry.addViewController("/").setViewName("forward:index.html");
}
}
but in master branch of Spring Boot, welcome page is configured differently with same result. I think that welcome page _(index.html)_ is not always under _/_ and it should be configurable, for example with new property spring.mvc.welcome-page-path=/ # Path pattern used for static resources
where default is actual behaviour but developer can change that welcome page is for example under _/console/v2/_.
From SPA point of view, _index.html_ is static assets too and can be served from non root path. I am only suggesting improvement on which path a welcome page can be registered by default = the root path or path from configuration which can be changed by anyone.
We discussed this today and it's our opinion that a page somewhere other than the root of the app is no longer a welcome page as you already need to know about a specific path to access it. As such, what you've described isn't something that we want Spring Boot's welcome page to support.
As you've noted above you can configure this yourself in a handful of lines of code and that's our recommended approach.
While we were discussing this, we realised that the current forward will result in a 404 if you've customised the static path pattern. I've opened #8788 to tighten that up a bit.