23

I was trying to find a way to change the default welcome-page for a spring-boot application that is being deployed as a war in production but I can't find a way to do it without a web.xml file.

According to the documentation we can do it using the EmbeddedServletContainerFactory with this code:

@Bean
public EmbeddedServletContainerFactory servletContainer() {

    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

    TomcatContextCustomizer contextCustomizer = new TomcatContextCustomizer() {
        @Override
        public void customize(Context context) {
            context.addWelcomeFile("/<new welcome file>");
        }
    };
    factory.addContextCustomizers(contextCustomizer);

    return factory;
}

Although, as we're creating a war file and deploying it to tomcat and not using the Embedded Tomcat, this isn't doing anything.

Any idea? If we really need to add a web.xml file, how can we do it and still using spring boot? Should we specify the Application bean(with the main method) as the application context for DispatcherServlet? The documentation isn't very clear about that.

Older Servlet containers don’t have support for the ServletContextInitializer bootstrap process used in Servlet 3.0. You can still use Spring and Spring Boot in these containers but you are going to need to add a web.xml to your application and configure it to load an ApplicationContext via a DispatcherServlet.

Thanks in advance!

Pedro

pVilaca
  • 1,508
  • 1
  • 12
  • 18
  • 2
    The fact that you have a web.xml doesn't mean the ServletContainerInitializer doesn't work anymore. Just drop a web.xml in the file with only a `welcome-page` tag. – M. Deinum Sep 26 '14 at 11:21

4 Answers4

30

It's not too hard to do... you just need to forward the default mapping...

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter{

    @Override
    public void addViewControllers( ViewControllerRegistry registry ) {
        registry.addViewController( "/" ).setViewName( "forward:/yourpage.html" );
        registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
        super.addViewControllers( registry );
    }
}
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
10

Well, a few years passed since the last answer - and code evolves..

This won't work on Spring 5 / Java 8+, you should implement the interface and override the default method.

import org.springframework.core.Ordered;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class DefaultViewConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/homepage.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}
Yossi
  • 1,056
  • 2
  • 13
  • 22
  • If you have any static resources loaded in homepage.html and they are under a sub path, you are better off adding a `forward:` prefix so you don't have to add special rules in spring-security allow url patters (if you have spring-security) – Anand Rockzz Jan 03 '23 at 09:41
9

Following Michael's tutorial, I was able to just map / to my index.gsp file.

@Controller
class Routes {

    @RequestMapping({
        "/",
        "/bikes",
        "/milages",
        "/gallery",
        "/tracks",
        "/tracks/{id:\\w+}",
        "/location",
        "/about"
    })
    public String index() {
        return "forward:/index.gsp";
    }
}
Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
  • This worked for me, had to change square brackets to curly braces but it solved my issue! – sharmaap Apr 06 '18 at 20:00
  • Didn't work for me. I got: `Could not resolve view with name 'forward:/index.jsp' in servlet with name 'dispatcherServlet'","path":"/"}` – Chris Wolf Mar 04 '20 at 21:08
0

I am doing it as follows.

package org.gwtproject.tutorial.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Configure the welcome page 
 * 
 */
@Configuration
public class SpringBootWelcomePageConfiguration extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    /**
     * redirect a user to the welcome page when he visits tha app without a
     * destination url.
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/ForExampleAGwtEntrypoint.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}
99Sono
  • 3,554
  • 27
  • 39