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>
<h1>首页</h1>
<div th:text="${msg}"></div>
</body>
</html>
错误页
文件位置:
classpath:/templates/error/404.html
classpath:/templates/error/4xx.html
classpath:/templates/error/500.html
classpath:/templates/error/5xx.html
SpringBoot 之 静态资源路径、显示首页、错误页的更多相关文章
- springboot之静态资源路径配置
静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取. 在Springboot中默认的静态资源路径有:classpath:/META-INF/resources/,classp ...
- Springboot 之 静态资源路径配置
1.静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户通过浏览器直接读取. 2.在Springboot中默认的静态资源路径有:classpath:/META-INF/resource ...
- springboot访问静态资源404
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- springboot配置静态资源访问路径
其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...
- SpringBoot:静态资源映射、定制404、配置icon
目录 静态资源映射规则 定制首页 定制错误页面 配置 icon 静态资源映射规则.定制首页.定制404页面.配置网站的图标 静态资源映射规则 SpringBoot中对于静态资源(css,js,img. ...
- SpringBoot 配置静态资源映射
SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...
- 13.1Springboot 之 静态资源路径配置
Spring 静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取. 在Springboot中默认的静态资源路径有:classpath:/META-INF/resources/ ...
- springboot下静态资源的处理(转)
在SpringBoot中有默认的静态资源文件相关配置,需要通过如下源码跟踪: WebMvcAutoConfiguration-->configureResourceChain(method)-- ...
随机推荐
- NSURLSession实现文件上传
7.1 涉及知识点(1)实现文件上传的方法 /* 第一个参数:请求对象 第二个参数:请求体(要上传的文件数据) block回调: NSData:响应体 NSURLResponse:响应头 NSErro ...
- 【Linux】【Basis】文件
refer to: https://en.wikipedia.org/wiki/POSIX refer to: https://en.wikipedia.org/wiki/Unix_file_type ...
- C# 温故知新 第二篇 C# 程序的通用结构
C# 程序由一个或多个文件组成. 每个文件均包含零个或多个命名空间. 一个命名空间包含类.结构.接口.枚举.委托等类型或其他命名空间. 以下示例是包含所有这些元素的 C# 程序主干. 主要包括 1. ...
- 判断存在…Contains…(Power Query 之 M 语言)
表函数 判断记录在表中是否存在 = Table.Contains( 表, 记录, {"指定列1",-, "指定列n"}) = Table.ContainsAll ...
- Windows10下mysql 8.0.19 安装配置方法图文教程
第一步 下载安装包: 官网 毕竟是甲骨文公司的产品,去官网下真的慢! 这里有两个供选择的,我建议选第一个(因为我先下了第二个,结果失败了,不知道为什么总是出错.) 下载完自行选择路径解压就可以了. 第 ...
- 🏆【CI/CD技术专题】「Docker实战系列」(1)本地进行生成镜像以及标签Tag推送到DockerHub
背景介绍 Docker镜像构建成功后,只要有docker环境就可以使用,但必须将镜像推送到Docker Hub上去.创建的镜像最好要符合Docker Hub的tag要求,因为在Docker Hub注册 ...
- CF200B Drinks 题解
Content 有 \(n\) 杯饮料,第 \(i\) 杯饮料中橙汁占 \(a_i\%\).现在请求出这 \(n\) 杯饮料混合成一杯饮料后的橙汁所占百分比. 数据范围:\(1\leqslant n\ ...
- CF1132B Discounts 题解
Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\).有 \(q\) 次询问,每次询问给定一个数 \(x\).对于每次询问,求出数组中去掉一个第 \(x\) ...
- 动态导入模块__import__("str") importlib标准库
解释器内部使用的为__import__('str') #!/usr/bin/env python # Author:Zhangmingda print('我是aa类 ') #被import的时候就执行 ...
- ConfigParser模块,主要应用于对php.ini等格式的配置文件内容读取和生成。删改较少用
*.ini配置文件样例 创建默认的文件样例 读取文件内容 read() configparser增删改查语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...