Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure session-config and error-page

后端 未结 5 2137
迷失自我
迷失自我 2020-12-14 18:19

WebApplicationInitializer provides a way to programmatically represent a good portion of a standard web.xml file - the servlets, filters, listeners.

However I have

5条回答
  •  旧时难觅i
    2020-12-14 18:35

    Using spring-boot it's pretty easy.

    I am sure it could be done without spring boot as well by extending SpringServletContainerInitializer. It seems that is what it is specifically designed for.

    Servlet 3.0 ServletContainerInitializer designed to support code-based configuration of the servlet container using Spring's WebApplicationInitializer SPI as opposed to (or possibly in combination with) the traditional web.xml-based approach.

    Sample code (using SpringBootServletInitializer)

    public class MyServletInitializer extends SpringBootServletInitializer {
    
        @Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory containerFactory = new TomcatEmbeddedServletContainerFactory(8080);
    
            // configure error pages
            containerFactory.getErrorPages().add(new ErrorPage(HttpStatus.UNAUTHORIZED, "/errors/401"));
    
            // configure session timeout
            containerFactory.setSessionTimeout(20);
    
            return containerFactory;
        }
    }
    

提交回复
热议问题