在单独的jar中的Spring MVC静态资源 [英] Spring MVC static resources in separate jar

查看:91
本文介绍了在单独的jar中的Spring MVC静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我有一个同事的疑问,我们是否可以将我们的静态资源(css,图像,js)放在单独的jar文件中,并从spring mvc应用程序访问此静态资源.我在Google上搜索,但没有发现任何有趣的东西.所以我的问题是,可能吗?如果可以的话,请您说明由谁来做.

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.

谢谢

推荐答案

Spring Boot服务器使用classpath:/resources/形式的静态资源, classpath:/static/classpath:/public/(还有更多). 因此,如果您创建一个具有所有静态资源的jar文件(例如,"public"),并将其放在类路径中,则将提供所有内容.

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.

如果不使用Boot,则可以配置应用程序以提供该文件夹中的资源:

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/");
}

这篇关于在单独的jar中的Spring MVC静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆