【Spring Boot 2.0学习之旅-02-6】SpringBoot2.x目录文件结构讲解

本文介绍SpringBoot中静态资源如CSS、JS、图片等的管理方法,包括文件存放位置、加载顺序及如何通过Thymeleaf展示HTML文件等内容。

第二章:SpringBoot接口Http协议开发实战

02-6 SpringBoot2.x目录文件结构讲解

 简介:讲解SpringBoot目录文件结构和官方推荐的目录规范

 1、目录讲解
	 src/main/java:存放代码
	 src/main/resources
	 	static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
	 	templates:存放静态页面jsp,html,tpl
	 	config:存放配置文件,application.properties
	 	resources:

 2、引入依赖 Thymeleaf
 	<dependency>
	   <groupId>org.springframework.boot</groupId>
	   <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	注意:如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,
	比如resources、static、public这个几个文件夹,才可以访问

 3、同个文件的加载顺序,静态资源文件
 Spring Boot 默认会挨个从
 META/resources > resources > static > public  里面找是否存在相应的资源,如果有则直接返回。

 4、默认配置	
 	1)官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

 	2)spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 

 5、静态资源文件存储在CDN

1.Spring Boot目录结构以及加载顺序

目录讲解
src/main/java:存放代码
src/main/resources
static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
templates:存放静态页面jsp,html,tpl
config:存放配置文件,application.properties
resources:

默认情况下,Spring Boot 提供来自类路径中名为/static(或/public/resources/META-INF/resources)的目录或从ServletContext. 它使用ResourceHttpRequestHandler来自 Spring MVC 的,以便您可以通过添加自己WebMvcConfigureraddResourceHandlers方法并覆盖该方法来修改该行为。

同个文件的加载顺序,静态资源文件 Spring Boot 默认会挨个从 META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。

例子:

在这里插入图片描述

访问

http://localhost:8080/css/main.css

在这里插入图片描述

http://localhost:8080/images/100.png

在这里插入图片描述

http://localhost:8080/js/test.js

在这里插入图片描述

2.通过Thymeleaf显示加载的HTML文件

在templates下创建index.html文件。具体如下图所示。

在这里插入图片描述

但如果直接访问的话,会报错。

在这里插入图片描述

首先需要在pom.xml中加入Thymeleaf依赖包信息。

在这里插入图片描述

注意:如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,
比如resources、static、public这个几个文件夹,才可以访问

之后在controller下新建一个HtmlController的文件。

package com.example.spring_demo03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author : codingchao
 * @date : 2021-11-17 12:45
 * @Description:
 **/
@Controller
public class HtmlController {
    @RequestMapping(value = "/api/html")
    public Object index() {
        return "index";
    }
}

在这里插入图片描述

3.自定义加载配置文件

默认配置:官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

自定义配置:

spring.web.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 

在这里插入图片描述

还有后续工作,大家点赞评论收藏关注走起来!!!学起来!!!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值