更改部署为war的spring-boot应用程序的默认欢迎页面 [英] Changing default welcome-page for spring-boot application deployed as a war

查看:166
本文介绍了更改部署为war的spring-boot应用程序的默认欢迎页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

根据文档,我们可以使用带有以下代码的EmbeddedServletContainerFactory来做到这一点:

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

尽管,当我们创建一个war文件并将其部署到tomcat而不使用嵌入式Tomcat时,这没有做任何事情.

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

有什么主意吗?如果确实需要添加一个web.xml文件,我们该如何做并且仍然使用spring boot?我们是否应该将Application bean(带有main方法)指定为DispatcherServlet的应用程序上下文?该文档对此不太清楚.

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.

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

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.

提前谢谢!

佩德罗

推荐答案

要做起来并不难...您只需要转发默认映射...

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

这篇关于更改部署为war的spring-boot应用程序的默认欢迎页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆