如何配置SpringBoot静态资源路径
小编这次要给大家分享的是如何配置SpringBoot静态资源路径,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
静态资源路径
静态资源支持放在以下路径中,访问优先级从上到下:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/ # 默认路径
classpath:/public/
其中 classpath 为 src/main/resources 目录。
请求地址为:http://localhost:8080/xx.js
首页
文件位置:
classpath:/static/favicon.ico
classpath:/templates/index.html
导入 thymeleaf 模板引擎依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency></dependencies>
定义请求控制器:
@Controllerpublic class IndexController { @RequestMapping({"/", "/index.html"}) public String index(Model model){ model.addAttribute("msg", "Hello, Thymeleaf!"); return "index"; }}
加入模板内容显示首页:
<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"> <title>index page</title></head><body><h2>首页</h2><div th:text="${msg}"></div></body></html>
看完这篇关于如何配置SpringBoot静态资源路径的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。