# 用 SpringBoot 开发简单的网站

springboot 到底帮我们配置了什么?我们能不能进行修改?能修改什么东西?能不能扩展?

xxxxAutoConfiguration… 向容器中自动配置组件

xxxxProperties: 自动配置类,装配配置文件中自定义的一些内容

要解决的问题:

导入静态资源…

首页

jsp,模板引擎 Thymelef

装配扩展 SpringMVC

增删改查

拦截器

目录名称 作用说明
config 配置类,放 Spring 配置、跨域配置、WebSocket 配置、安全配置等。
controller 控制层,负责接收前端请求,调用 service,返回数据或视图。
service 业务逻辑层,处理具体的业务操作。通常是 controller 调用它。
model/entity 实体类,对应数据库表的字段结构。
repository/dao 数据访问层,用来操作数据库,一般继承 JpaRepositoryCrudRepository
utils 工具类,比如日期处理、字符串处理、JWT 工具等。

# 📁 src/main/resources

这是 Spring Boot 的资源文件目录。

文件 / 目录 作用说明
application.properties / application.yml 配置文件,比如端口、数据库、日志、缓存等配置。
templates/ 放 Thymeleaf 或 Freemarker 的 HTML 模板页面(必须要放在这儿才能渲染页面)。
static/ 放静态资源:CSS、JS、图片(这些不会被 Spring MVC 拦截,直接访问)。
public/ 也可以放静态资源,效果和 static 一样。
banner.txt 启动 Spring Boot 时控制台打印的图标,可以自定义。

# 📁 src/test/java

写单元测试的地方,比如使用 JUnit、Mockito 写服务层、控制器的测试。


# 📁 target/

Maven 或 Gradle 构建后生成的文件目录,不用手动修改。打包后的 .jar 文件也在这里。


# 📄 项目根目录下的文件

文件名 作用
pom.xml Maven 项目的依赖管理文件,添加第三方库就靠它。
build.gradle 如果你用 Gradle 管理项目,则是用这个文件。
.gitignore 忽略不需要 Git 提交的文件,比如 target/.idea/
README.md 项目说明文档。

# 、静态资源

直接全局搜索:

WebMvcAutoConfigguration.class(自动配置类)

image-20250415191709161

WebMvcAutoConfigurationAdapter

在这个类里面找到 addResourceHandlers 这个增加资源处理器类

public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");} else {this.addResourceHandler(registry,this.mvcProperties.getWebjarsPathPattern(), "classpath:/META-INF/resources/webjars/");this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {ServletContextResource resource = new ServletContextResource(this.servletContext, "/");     registration.addResourceLocations(new Resource[]{resource});}});
            }
        }

image-20250415192249014

找到静态资源 path

private String staticPathPattern = "/**";

在这里找到

@ConfigurationProperties(
    prefix = "spring.mvc"
)

分析 addResourceHandlers 这个方法

if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");}

是如果自己在 application.properties 里面加上

spring.mvc.static-path-pattern=路径

是自己添加静态资源的路径

若不自己给路径:

else {
                this.addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(), "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());

这里就是默认的路径,但是在我们自己创建的文件中没见到 webjars 目录

image-20250415193204101

这里就是 webjars 文件的导入,可以在浏览器中直接搜索 webjars

https://www.webjars.org/

image-20250415193327384

这里

在 pom,xml 这里用来导入依赖就好了

<dependencies>在这里加入要加的依赖就行了</dependencies>

image-20250415204215473

在静态资源这里可以直接访问网站里面导入包的文件

if (this.servletContext != null) {ServletContextResource resource = new ServletContextResource(this.servletContext, "/");     registration.addResourceLocations(new Resource[]{resource});}});

最后就是这个

# 模板引擎 themeleaf

依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

这个可以在 templates 目录下有一个 index.html

在部署后 http://localhost:8080 这个下有一个默认的 index.html 就是这个 index.html

想访问 templates 下的其他 html 就得在 Controller 目录下写 java 来处理请求

简单就是:

package com.studyweb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class _12333Controller {
    @GetMapping("/12333")
    public String index(){
        return "12333";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>sqwe</h1>
</body>
</html>

image-20250416194243305

# Thymeleaf 模板语法:

th:each循环
th:if if语句

提取文本:th:text   /   th:utext(文本里的标签也会解析)
 

示例:

<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3 th:each="user:${users}" [[$user]]></h3>

这个将 users 的东西用 user 遍历一遍,将每一次遍历的对象用位版本提取出来

# 装配扩展 SpringMVC

感觉这个就是在 model 创建一个对象,我能在 repository 这里继承 JpaRepository:

JpaRepository 是 Spring Data JPA 提供的一个接口,它是用来简化数据库访问操作的。它本身是一个泛型接口,继承自 PagingAndSortingRepository 和 CrudRepository,也就是说它不仅支持基本的增删改查(CRUD)功能,还支持分页和排序。

然后可以用里面的一个方法:

JpaRepository<T, ID> 提供了很多现成的方法,比如:

save(entity):保存或更新一条记录

findById(id):根据主键查找

findAll():查找所有记录

deleteById(id):根据主键删除

count():统计记录总数

existsById(id):判断某条记录是否存在

# 重新开始:

Edited on

Give me a cup of [coffee]~( ̄▽ ̄)~*

odiws WeChat Pay

WeChat Pay

odiws Alipay

Alipay

odiws PayPal

PayPal