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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26057995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:05:29  来源:igfitidea点击:

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

springtomcat7spring-boot

提问by pVilaca

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.

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

According to the documentation we can do it using the EmbeddedServletContainerFactory with this code:

根据文档,我们可以使用 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;
}

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

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

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.

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

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.

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

Thanks in advance!

提前致谢!

Pedro

佩德罗

回答by Eddie B

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

回答by Nick Grealy

Following Michael's tutorial, I was able to just map /to my index.gspfile.

按照迈克尔的教程,我能够映射/到我的index.gsp文件。

@Controller
class Routes {

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

回答by Yossi

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.

这不适用于 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);
    }
}

回答by 99Sono

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