侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

静态资源

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 113 阅读 / 2,957 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/WEB-INF/wjHtml/**" location="/WEB-INF/wjHtml/"/>

访问路径 -> 目录 文件
baseUrl/js/ -> webapp/js/




在springBoot下静态资源默认有4个位置,
问题: css/asd.css 经过mvc已处理路径就剩下asd.css了,然后又去静态资源根文件夹去找,所以找不到
解决方法: 就是把 .addResourceLocations("classpath:/static/", "classpath:/static/js/", "classpath:/static/css/", "classpath:/static/img/", "classpath:/static/fonts/")


static
	css
	fonts
	img
	js
	favicon.ico
	index.html
package com.learn.config;

import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

@Override
public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
    FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
    fastJsonConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
    FastJsonConfig config = new FastJsonConfig();
    config.setFeatures(Feature.AllowISO8601DateFormat, Feature.DisableCircularReferenceDetect);

    SerializeConfig serializeConfig = new SerializeConfig();
    serializeConfig.setPropertyNamingStrategy(PropertyNamingStrategy.CamelCase);
    config.setSerializeConfig(serializeConfig);

// config.setDateFormat("yyyy");
// ParserConfig parserConfig = new ParserConfig();
// config.setParserConfig(parserConfig);

    fastJsonConverter.setFastJsonConfig(config);

}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(&quot;/js/**&quot;, &quot;/css/**&quot;, &quot;/img/**&quot;, &quot;/fonts/**&quot;, &quot;index.html&quot;, &quot;favicon.ico&quot;)
            .addResourceLocations(&quot;classpath:/static/&quot;, &quot;classpath:/static/js/&quot;, &quot;classpath:/static/css/&quot;, &quot;classpath:/static/img/&quot;, &quot;classpath:/static/fonts/&quot;)

// .addResourceLocations("file:D://static")
.setCachePeriod(31556926);
}
}

0

评论区