SpringBoot在项目中访问静态资源步骤分析
作者:xl649138628 时间:2022-12-13 08:04:31
在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";
}
}
总结:
按照上面的配置后我们就可以访问到静态资源。
来源:https://blog.csdn.net/xl649138628/article/details/128742158
标签:SpringBoot,访问,静态,资源
0
投稿
猜你喜欢
Unity3D实现人物移动示例
2022-08-15 21:51:58
Struts2中接收表单数据的三种驱动方式
2022-04-21 09:23:11
C# Volatile的具体使用
2023-11-21 11:14:50
Unity通过脚本创建网格Mesh的方法
2023-02-26 23:38:00
关于@Autowired注解和静态方法及new的关系
2021-07-16 13:08:06
c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)
2022-02-27 06:53:11
Java中如何使用Response重定向
2023-08-03 07:41:07
解决idea check out 切换分支时找不到需要的分支问题
2023-04-04 09:12:57
MyBatis 中 SqlMapConfig 配置文件详解
2023-10-19 21:23:01
JavaFx Tooltip悬浮提示使用及自定义代码详解
2023-05-11 15:06:05
Mybatis控制台打印Sql语句的实现代码
2021-10-08 22:06:28
C#调用WebService实例开发
2022-11-21 22:51:08
netty pipeline中的inbound和outbound事件传播分析
2023-08-27 06:57:00
一文带你弄懂Java中线程池的原理
2023-09-10 20:59:02
C#编程实现动态改变配置文件信息的方法
2022-05-01 15:09:25
Android Activity生命周期调用的理解
2023-05-13 14:15:42
使用 CliWrap 让C#中的命令行交互(推荐)
2023-04-19 01:31:51
详解Spring-boot中读取config配置文件的两种方式
2021-07-04 15:52:55
Java实现简易Web服务器
2023-11-17 09:10:11
Java中ByteArrayInputStream和ByteArrayOutputStream用法详解
2023-03-01 11:22:48