Spring MVC static resources in separate jar

2019-03-01 21:05发布

问题:

Recently I had a question from my co-workers if we can put our static resources(css, images, js) in separate jar file and access this static resources from spring mvc application. I was looking on google but i didn't find anything interesting. So my question is, is possible? and if so, can you please explain who to do it.

Thank you,

回答1:

Spring Boot servers the static resources form classpath:/resources/, classpath:/static/ and classpath:/public/ (and some more). So if you create a jar file with one folder in it("public" for example) with all your static resources and put that jar in the classpath, all that content will be served.

If you don't use Boot, you can configure your application to serve the resources from that folder:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
      .addResourceHandler("/resources/**")
      .addResourceLocations("/resources/","classpath:/public/");
}