How to define and in servlet 3.0's web.xml-less?

后端 未结 3 2561
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 19:36

I have existing web-app which I want to convert into web.xml-less of servlet\'s 3.0. I\'ve managed to make it working, however there are 2 tags in web.xml which I still don\

3条回答
  •  爱一瞬间的悲伤
    2020-11-27 20:09

    In Spring Boot or general Spring MVC app for following scenario:

    Static files can be served from locations registered with a custom ResourceHandlerRegistry. We have a static resource index.html and it can accessed at localhost:8080/index.html. We want to just redirect localhost:8080/ request to localhost:8080/index.html, following code will can be used.

    package in.geekmj.config;
    
    import org.springframework.context.annotation.Configuration;
    
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @EnableWebMvc
    public class WebConfiguration extends WebMvcConfigurerAdapter {
    
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
            "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/index.html");
    }
    }
    

    Now accessing localhost:8080/ will redirect to localhost:8080/index.html

提交回复
热议问题