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

查看:27
本文介绍了更改作为战争部署的 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 );
    }
}

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

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