Spring Boot with AngularJS html5Mode

后端 未结 9 2304
长发绾君心
长发绾君心 2020-11-27 04:35

I start my web application with spring boot. It use a simple main class to start an embedded tomcat server:

@Configuration
@EnableAutoConfiguration
@Componen         


        
9条回答
  •  情书的邮戳
    2020-11-27 04:50

    A small adjustment to a previous code which works to me.

    // Running with Spring Boot v1.3.0.RELEASE, Spring v4.2.3.RELEASE
    @Configuration
    @EnableConfigurationProperties({ ResourceProperties.class })
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    @Autowired
    private ResourceProperties resourceProperties = new ResourceProperties();
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        Integer cachePeriod = resourceProperties.getCachePeriod();
    
        final String[] staticLocations = resourceProperties.getStaticLocations();
        final String[] indexLocations  = new String[staticLocations.length];
        for (int i = 0; i < staticLocations.length; i++) {
            indexLocations[i] = staticLocations[i] + "index.html";
        }
        registry.addResourceHandler(
                "/**/*.css",
                "/**/*.html",
                "/**/*.js",
                "/**/*.json",
                "/**/*.bmp",
                "/**/*.jpeg",
                "/**/*.jpg",
                "/**/*.png",
                "/**/*.ttf",
                "/**/*.eot",
                "/**/*.svg",
                "/**/*.woff",
                "/**/*.woff2"
                )
                .addResourceLocations(staticLocations)
                .setCachePeriod(cachePeriod);
    
        registry.addResourceHandler("/**")
                .addResourceLocations(indexLocations)
                .setCachePeriod(cachePeriod)
                .resourceChain(true)
                .addResolver(new PathResourceResolver() {
                    @Override
                    protected Resource getResource(String resourcePath,
                            Resource location) throws IOException {
                        return location.exists() && location.isReadable() ? location
                                : null;
                    }
                });
    }
    

    }

提交回复
热议问题