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

问题描述 投票:16回答:4

我试图找到一种方法来更改一个弹簧启动应用程序的默认欢迎页面,该应用程序在生产中被部署为战争,但是如果没有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而不使用Embedded Tomcat,但这并没有做任何事情。

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

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

提前致谢!

佩德罗

spring tomcat7 spring-boot
4个回答
25
投票

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

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

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
投票

好吧,自最后一个答案以来已经过去了几年 - 代码也在不断发展......

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

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.