集团站切换校区

验证码已发送,请查收短信

复制成功
微信号:togogoi
添加微信好友, 详细了解课程
已复制成功,如果自动跳转微信失败,请前往微信添加好友
打开微信
图标

学习文章

当前位置:首页 > >学习文章 > >

{SpringBoot}web开发

发布时间: 2019-01-16 17:17:08

​springboot引入静态资源

ctrl+n 全局搜索 (WebMvc)

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

if (!this.resourceProperties.isAddMappings()) {

logger.debug("Default resource handling disabled");

return;

}

Duration cachePeriod = this.resourceProperties.getCache().getPeriod();

CacheControl cacheControl = this.resourceProperties.getCache()

.getCachecontrol().toHttpCacheControl();

if (!registry.hasMappingForPattern("/webjars/**")) {

customizeResourceHandlerRegistration(registry

.addResourceHandler("/webjars/**")

.addResourceLocations("classpath:/META-INF/resources/webjars/")

.setCachePeriod(getSeconds(cachePeriod))

.setCacheControl(cacheControl));

}

String staticPathPattern = this.mvcProperties.getStaticPathPattern();

if (!registry.hasMappingForPattern(staticPathPattern)) {

customizeResourceHandlerRegistration(

registry.addResourceHandler(staticPathPattern)

.addResourceLocations(getResourceLocations(

this.resourceProperties.getStaticLocations()))

.setCachePeriod(getSeconds(cachePeriod))

.setCacheControl(cacheControl));

}

}


//配置欢迎页面

@Bean

public WelcomePageHandlerMapping welcomePageHandlerMapping(

ResourceProperties resourceProperties) {

return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),

this.mvcProperties.getStaticPathPattern());

}


//配置喜欢的图标

@Configuration

@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)

public static class FaviconConfiguration {


private final ResourceProperties resourceProperties;


public FaviconConfiguration(ResourceProperties resourceProperties) {

this.resourceProperties = resourceProperties;

}


@Bean

public SimpleUrlHandlerMapping faviconHandlerMapping() {

SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();

mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);

//所有  **/favicon.ico

mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",

faviconRequestHandler()));

return mapping;

}


@Bean

public ResourceHttpRequestHandler faviconRequestHandler() {

ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();

requestHandler

.setLocations(this.resourceProperties.getFaviconLocations());

return requestHandler;

}

}

所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

​webjars:以jar包的方式引入静态资源;​

https://www.webjars.org/ 找到需要引入的资源文件 写到pom中:​

<dependency>

   <groupId>org.webjars</groupId>

   <artifactId>jquery</artifactId>

   <version>3.3.1</version>

</dependency>


​http://localhost:8080/webjars/jquery/3.3.1/jquery.js 即可访问jquery;

​自定义的静态资源存放:ResourceProperties​

"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找;

{

"classpath:/META-INF/resources/", "classpath:/resources/",

"classpath:/static/", "classpath:/public/" };

"/":当前项目的根路径


自定义图标

命名为favicon.ico,放在静态文件夹下即可!

自定义静态文件存放路径:spring.resources.static-locations=classpath:/qmk

上一篇: {HTML5}DOM节点操作-第三节

下一篇: {HTML5}DOM节点操作-第二节

十五年老品牌
微信咨询:togogoi 咨询电话:18922156670 咨询网站客服:在线客服

相关课程推荐

在线咨询 ×

您好,请问有什么可以帮您?我们将竭诚提供最优质服务!