Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing default welcome-page for spring-boot application deployed as a war

Tags:

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

like image 250
pVilaca Avatar asked Sep 26 '14 10:09

pVilaca


People also ask

How do I change my default spring boot?

The fastest and easiest way to customize Spring Boot is by overriding the values of the default properties. For the server port, the property we want to change is server. port. By default, the embedded server starts on port 8080.

Does spring boot use war to deploy?

By using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.


2 Answers

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 );     } } 
like image 105
Edward J Beckett Avatar answered Oct 06 '22 10:10

Edward J Beckett


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";     } } 
like image 45
Nick Grealy Avatar answered Oct 06 '22 09:10

Nick Grealy