博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.对静态资源映射的规则
阅读量:7042 次
发布时间:2019-06-28

本文共 3954 字,大约阅读时间需要 13 分钟。

对静态资源映射的映射类配置:

public void addResourceHandlers(ResourceHandlerRegistry registry) {    if (!this.resourceProperties.isAddMappings()) {        logger.debug("Default resource handling disabled");    } else {        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();        if (!registry.hasMappingForPattern("/webjars/**")) {            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{
"/webjars/**"}). addResourceLocations(new String[]{
"classpath:/META-INF/resources/webjars/"}). setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }}

 

 

1)、 webjars

所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
webjars:以jar包的方式引入静态资源;
 
网址:

 

访问网址进行一下测试:

可以成功访问

 

ResourceProperties.java

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)public class ResourceProperties implements ResourceLoaderAware {  //可以设置和静态资源有关的参数,缓存时间等 ..... }

 

在访问的时候只需要写webjars下面资源的名称即可

具体的引入maven再官网可进行查询

org.webjars
jquery
3.3.1

 

此时引入之后就可以使用!!!!

 

2)、存放静态资源的位置

"/**" 访问当前项目的任何资源,都去(
静态资源的文件夹)找映射
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"};"/" 当前项目根路径

 

在resources目录下的:
-resources
-static
-public
-/

 

测试访问hello.html网页

可以直接找到网页文件的位置 

 

 3)、配置欢迎页

 都在静态资源文件夹下....

@Configuration@ConditionalOnProperty(    value = {
"spring.mvc.favicon.enabled"}, matchIfMissing = true)public static class FaviconConfiguration implements ResourceLoaderAware { private final ResourceProperties resourceProperties; private ResourceLoader resourceLoader; public FaviconConfiguration(ResourceProperties resourceProperties) { this.resourceProperties = resourceProperties; } public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(-2147483647); mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler())); return mapping; } @Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler(); requestHandler.setLocations(this.resolveFaviconLocations()); return requestHandler; } private List
resolveFaviconLocations() { String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.   getResourceLocations(this.resourceProperties.getStaticLocations()); List
locations = new ArrayList(staticLocations.length + 1); Stream var10000 = Arrays.stream(staticLocations); ResourceLoader var10001 = this.resourceLoader; this.resourceLoader.getClass(); var10000.map(var10001::getResource).forEach(locations::add); locations.add(new ClassPathResource("/")); return Collections.unmodifiableList(locations); }}

所有的 **/favicon.ico 都是在静态资源文件下找;

 与之前的进行对比

 

5)、配置文件的方式

spring.resources.static-locations=classpath:/hello,classpath:/qwe

实质是一个数组的形式,该配置之后静态文件不能访问

 

 此时原来静态文件的文件

 

转载于:https://www.cnblogs.com/Mrchengs/p/10339338.html

你可能感兴趣的文章
mysql主从配置
查看>>
Android生命周期图文介绍
查看>>
初涉 Git 心得
查看>>
私有VLAN知识点
查看>>
Shell 脚本介绍
查看>>
Open×××的服务器端和客户端搭建(二)
查看>>
Redis 键(key)相关的命令及其它命令的查看地址
查看>>
vm虚拟机怎么访问本地硬盘
查看>>
Bootstrap3 排版-页面主体
查看>>
JAVA面向对象-----成员内部类访问细节
查看>>
《敏捷软件开发过程及最佳实践》培训总结
查看>>
Python最简编码规范
查看>>
Repeater,ItemDataBound事件,获取绑定列的值,给指定列添加js方法
查看>>
降维中的特征选择
查看>>
如何在ChemDraw中缩短双键长度
查看>>
【tarjan+lca】有机化学之神偶尔会做作弊
查看>>
Android之permission权限列表(AndroidManifest.xml)
查看>>
Redux入门学习
查看>>
我的友情链接
查看>>
利用AWS boto实现EC2 存储卷的自动快照
查看>>