Changing default URL mapping for Serving Static Content in Spring Boot

后端 未结 5 1670
鱼传尺愫
鱼传尺愫 2021-01-19 13:29

My static resources stopped working as soon as I added a new Controller (non rest) in my application with the following mapping

@RequestMapping(value = \"/{p         


        
5条回答
  •  囚心锁ツ
    2021-01-19 14:07

    For no controller pages:

    @Controller
    @RequestMapping("/feature")
    public class DataTableController {
    
        // map /feature/* to /feature/*
        @RequestMapping(value="/{name}", method = RequestMethod.GET)
        public ModelAndView staticPage(@PathVariable String name){
            return new ModelAndView("feature/" + name);
        }
    
    }
    

    For static resource except for HTML:

    @EnableWebMvc
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        // map /res/ to classpath:/resources/static/
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/res/**").addResourceLocations("classpath:/static/");
        }
    }
    

提交回复
热议问题