我试图找到一种方法来更改作为生产中的 war 部署的 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 的应用程序上下文?文档对此不是很清楚。
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.
提前致谢!
佩德罗
最佳答案
做起来并不难……只需要转发默认映射就行了……
@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 - 更改部署为 war 的 spring-boot 应用程序的默认欢迎页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057995/