【问题标题】:Changing default welcome-page for spring-boot application deployed as a war更改部署为战争的 spring-boot 应用程序的默认欢迎页面
【发布时间】:2014-11-21 08:56:22
【问题描述】:

我试图找到一种方法来更改作为生产中的战争部署的 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 的应用程序上下文?文档对此不是很清楚。

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

提前致谢!

佩德罗

【问题讨论】:

  • 拥有 web.xml 的事实并不意味着 ServletContainerInitializer 不再工作。只需将 web.xml 放入文件中,其中只有一个 welcome-page 标记。

标签: spring tomcat7 spring-boot


【解决方案1】:

做起来并不难……只需要转发默认映射就行了……

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

【讨论】:

  • 我认为它已被弃用。
【解决方案2】:

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

【讨论】:

  • 这对我有用,必须将方括号更改为花括号,但它解决了我的问题!
  • 对我不起作用。我得到了:Could not resolve view with name 'forward:/index.jsp' in servlet with name 'dispatcherServlet'","path":"/"}
【解决方案3】:

好吧,距离上一个答案已经过去了几年 - 代码也在不断发展..

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

【讨论】:

    【解决方案4】:

    我是这样做的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2017-12-21
      • 2015-09-07
      相关资源
      最近更新 更多