如何配置SpringBoot静态资源路径

发布时间:2020-07-30 11:29:24 作者:小猪
来源:亿速云 阅读:136

小编这次要给大家分享的是如何配置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>

定义请求控制器:

@Controller
public 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静态资源路径的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

推荐阅读:
  1. SpringBoot配置Jsp和静态资源访问
  2. springboot怎样获取相对路径文件夹下静态资源

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot bo

上一篇:php require有哪些用法

下一篇:python闭包指的是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》