SpringBoot在项目中访问静态资源步骤分析
在springboot项目中如果要在不集成templates的情况下访问静态资源需要做以下配置
1.在项目的application.yml文件中做如下配置
spring:
profiles:
active: dev
mvc:
view:
prefix: /
suffix: .html
重点在
配置后生成为WebMvcProperties 配置类。该配置类中有一个内部类View
@ConfigurationProperties(prefix = "spring.mvc") public class WebMvcProperties {
View类可以配置视图的前缀和后缀
public static class View { /** * Spring MVC view prefix. 前缀 */ private String prefix; /** * Spring MVC view suffix. 后缀 */ private String suffix;
2.在项目的resource路径下新建文件夹
在ResourceHttpRequestHandler类的getResource方法中调用了getLocations()方法。
protected Resource getResource(HttpServletRequest request) throws IOException { String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); if (path == null) { throw new IllegalStateException("Required request attribute '" + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set"); } path = processPath(path); if (!StringUtils.hasText(path) || isInvalidPath(path)) { if (logger.isTraceEnabled()) { logger.trace("Ignoring invalid resource path [" + path + "]"); } return null; } if (isInvalidEncodedPath(path)) { if (logger.isTraceEnabled()) { logger.trace("Ignoring invalid resource path with escape sequences [" + path + "]"); } return null; } ResourceResolverChain resolveChain = new DefaultResourceResolverChain(getResourceResolvers()); //重点关注此处 Resource resource = resolveChain.resolveResource(request, path, getLocations()); if (resource == null || getResourceTransformers().isEmpty()) { return resource; } ResourceTransformerChain transformChain = new DefaultResourceTransformerChain(resolveChain, getResourceTransformers()); resource = transformChain.transform(request, resource); return resource; }
getLocations()方法返回的locations来自与springboot项目,其中时配置类ResourceProperties赋值。赋值的数据为
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
四个路径
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" }; /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */ private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
所以要访问静态资源需要配置到这四个路径下,如果所示
3.API端按如下编码
@Controller public class PageApi { @GetMapping({"/", "/index"}) public String toPage(String id){ return "index"; } }
总结:
按照上面的配置后我们就可以访问到静态资源。
到此这篇关于SpringBoot在项目中访问静态资源步骤分析的文章就介绍到这了,更多相关SpringBoot访问静态资源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java微信公众平台开发(2) 微信服务器post消息体的接收
这篇文章主要为大家详细介绍了Java微信公众平台开发第二步,微信服务器post消息体的接收,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04SpringCloud HystrixDashboard服务监控详解
Hystrix Dashboard 是Spring Cloud中查看Hystrix实例执行情况的一种仪表盘组件,支持查看单个实例和查看集群实例,本文将对其服务监控学习2022-11-11Springboot整合MongoDB的Docker开发教程全解
这篇文章主要介绍了Springboot整合MongoDB的Docker开发,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值 ,需要的朋友可以参考下2020-07-07
最新评论