Where to put static files such as CSS in a spring-boot project?

后端 未结 10 2039
猫巷女王i
猫巷女王i 2020-12-02 09:47

In my current spring-boot project, my views have this line:


to reference a static css

10条回答
  •  一向
    一向 (楼主)
    2020-12-02 10:15

    I use Spring-Boot 2.0.2

    I still keep @EnableWebMvc annotation in my webConfig.java and override addResourceHandlers() method to help browser reaches css and javascript files through http request.

    This is the webapp directory's structure

    directory structure.

    webConfig.java

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "example.com")
    public class WebConfig implements WebMvcConfigurer {
    
        // =======================================
        // =             Bean Config             =
        // =======================================
    
        @Bean
        public InternalResourceViewResolver getInternalResourceViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
    
            return resolver;
        }
    
    
        // =======================================
        // =          Override Methods           =
        // =======================================
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pdfs/**")
                    .addResourceLocations("/WEB-INF/pdfs/");
    
            registry.addResourceHandler("/css/**")
                    .addResourceLocations("/WEB-INF/css/");
    
            registry.addResourceHandler("/js/**")
                    .addResourceLocations("/WEB-INF/js/");
        }
    }
    

    index.jsp head tag:

    
        Title
    
        
        
    
    

    hope it helps you.

提交回复
热议问题