更改部署为战争的spring-boot应用程序的默认欢迎页面

时间:2014-09-26 10:59:26

标签: spring tomcat7 spring-boot

我试图找到一种方法来更改spring-boot应用程序的默认欢迎页面,该应用程序正在生产中作为战争部署,但是如果没有web.xml文件,我找不到办法。

根据文档,我们可以使用EmbeddedServletContainerFactory和以下代码来完成:

@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;
}

虽然,因为我们正在创建一个war文件并将其部署到tomcat而不使用嵌入式Tomcat,但这并没有做任何事情。

有什么想法吗?如果我们真的需要添加一个web.xml文件,我们怎么做呢仍然使用spring boot?我们是否应该将Application bean(使用main方法)指定为DispatcherServlet的应用程序上下文?文档对此并不十分清楚。

  

较旧的Servlet容器不支持Servlet 3.0中使用的ServletContextInitializer引导过程。您仍然可以在这些容器中使用Spring和Spring Boot,但是您需要将web.xml添加到应用程序并将其配置为通过DispatcherServlet加载ApplicationContext。

提前致谢!

佩德罗

4 个答案:

答案 0 :(得分:24)

做起来并不难...你只需转发默认的映射......

@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 );
    }
}

答案 1 :(得分:7)

关注Michael's tutorial后,我能够将/映射到我的index.gsp文件。

@Controller
class Routes {

    @RequestMapping({
        "/",
        "/bikes",
        "/milages",
        "/gallery",
        "/tracks",
        "/tracks/{id:\\w+}",
        "/location",
        "/about"
    })
    public String index() {
        return "forward:/index.gsp";
    }
}

答案 2 :(得分:0)

我这样做。

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);
    }
}

答案 3 :(得分:0)

好吧,距离最后一个答案已经过去了几年-代码不断发展。

这在Spring 5 / Java 8+上不起作用,您应该实现接口并覆盖默认方法。

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);
    }
}