广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot在项目中访问静态资源步骤分析
  • 741
分享到

SpringBoot在项目中访问静态资源步骤分析

SpringBoot访问静态资源SpringBoot访问资源SpringBoot资源访问 2023-01-28 12:01:37 741人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

在SpringBoot项目中如果要在不集成templates的情况下访问静态资源需要做以下配置 1.在项目的application.yml文件中做如下配置 spring: 

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 {
		
		private String prefix;
		
		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/" };
	
	private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

所以要访问静态资源需要配置到这四个路径下,如果所示

3.api端按如下编码

@Controller
public class PageApi {
    @GetMapping({"/", "/index"})
    public String toPage(String id){
        return "index";
    }
}

总结

按照上面的配置后我们就可以访问到静态资源。

到此这篇关于SpringBoot在项目中访问静态资源步骤分析的文章就介绍到这了,更多相关SpringBoot访问静态资源内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot在项目中访问静态资源步骤分析

本文链接: https://www.lsjlt.com/news/178491.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作