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

前端 未结 4 1966
梦谈多话
梦谈多话 2020-12-01 08:21

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 wi

相关标签:
4条回答
  • 2020-12-01 08:44

    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";
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:45

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:56

    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 );
        }
    }
    
    0 讨论(0)
  • 2020-12-01 09:00

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题