springboot 到底帮我们配置了什么?我们能不能进行修改?能修改哪些东西?能不能扩展?
xxxxAutoConfiguraion:向容器中自动配置组件xxxxProperties:自动配置类,装配配置文件中自定义的一些内容
一、访问静态资源
总结:
1. 在springboot, 我们可以使用以下方式处理静态资源
○ webjars localhost:8080/webjars/
○ public, static, /**, resources localhost:8080/
- 本地静态资源目录优先级
resources>static: >public
Spring Boot 会自动扫描以下目录,用于存放本地静态资源(CSS、JS、图片等):
/resources//static/(最常用)/public//META-INF/resources/
访问路径 :localhost:8080/(直接访问文件名,无需加目录名)例如:
- 静态文件
classpath:/static/css/style.css - 访问路径:
http://localhost:8080/css/style.css
当多个目录下存在同名静态文件时,加载优先级为:resources > static > public
- resources:优先级最高,适合存放业务相关的静态资源。
- static:默认推荐目录,通常存放全局静态资源。
- public:优先级最低,适合存放公共资源或测试文件。
二、Thymeleaf模板引擎
结论:只要需要使用 thymeleaf,只需要导入对应的依赖就可以了!我们将 html 放在我们的 templates 目录下即可。
(一)T hymeleaf 语法
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--所有的html元素都可以被 thymeleaf替换接管: th:元素名-->
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
<hr>
<!--th:each="user:${users}" Thymeleaf循环语法,会遍历users集合中的每个元素,将当前元素赋值给变量user。
[[ ${user} ]] 是Thymeleaf的内联文本语法,和th:text="${user}"完全一样,都是用来显示变量值。
通过 model.addAttribute传递了一个包含"123" 和 "abc" 的列表,页面会生成两个<h3>标签,分别显示这两个值。 -->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<!--<h3 th:each="user:${users}" >[[ ${user} ]]</h3>-->
</body>
</html>
css
// 在templates目录下的所有页面,只能通过controller来跳转
// 这个需要模板引擎的支持! thymeleaf
@Controller
public class TestController {
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("msg", "<h1>hello,springboot</h1>");
model.addAttribute("users", Arrays.asList("123", "abc"));
return "test";
}
}
(二)不同项目欢迎页面的配置
Spring Boot、Spring MVC 以及其他常用框架(如 SSM 整合、Spring Boot 自定义欢迎页) 的欢迎页配置方式:
1.Spring Boot 欢迎页配置(最常用)
Spring Boot 对欢迎页有自动配置,默认规则如下:
方式 1:默认静态资源欢迎页
直接在静态资源目录下放置 index.html,Spring Boot 会自动识别:
-
静态资源目录:
src/main/resources/static/、public/、resources/等(优先级:resources > static > public) -
示例:
src/main/resources/static/index.html -
访问:启动项目后直接访问
http://localhost:8080/
方式 2:自定义欢迎页(通过 Controller 跳转)
如果需要动态欢迎页(如带后端数据),通过 Controller 映射 / 路径:
java
@Controller
public class WelcomeController {
@RequestMapping("/")
public String welcome(Model model) {
model.addAttribute("msg", "Spring Boot 欢迎页");
return "index"; // 跳转到 templates/index.html(需模板引擎支持)
}
}
- 要求:
templates/index.html存在,且已引入 Thymeleaf/FreeMarker 等模板依赖。
方式 3:通过配置文件指定(Spring Boot 2.x+)
在 application.properties 中配置:
java
# 自定义欢迎页路径(针对静态资源)
spring.web.resources.static-locations=classpath:/my-static/
# 此时欢迎页放在 my-static/index.html 即可
2.Spring MVC 欢迎页配置(传统 XML 配置)
Spring MVC 没有自动配置,需手动在 spring-mvc.xml 中配置:
方式 1:静态资源欢迎页(配置 <mvc:default-servlet-handler>)
-
在
webapp目录下放置index.html(路径:webapp/index.html) -
在
spring-mvc.xml中开启默认 Servlet 处理静态资源:
java
<!-- 开启静态资源默认处理(让 Tomcat 处理静态资源) -->
<mvc:default-servlet-handler/>
- 访问:
http://localhost:8080/项目上下文路径/
方式 2:通过 Controller 跳转欢迎页
- 编写 Controller 映射
/路径:
java
@Controller
public class WelcomeController {
@RequestMapping("/")
public String welcome() {
return "index"; // 跳转到 WEB-INF/views/index.jsp(需配置视图解析器)
}
}
- 在
spring-mvc.xml配置视图解析器:
java
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
- 要求:
WEB-INF/views/index.jsp存在。
3.其他常用场景
- SSM 整合(Spring + Spring MVC + MyBatis)
同 Spring MVC 的配置方式,需注意:
-
静态资源欢迎页:
webapp/index.html+<mvc:default-servlet-handler/> -
动态欢迎页:Controller 映射
/+ 视图解析器配置
- Spring Boot 整合 JSP 的欢迎页
Spring Boot 不推荐 JSP,但如需使用:
-
在
webapp/WEB-INF/jsp/下放置index.jsp -
配置视图解析器(
application.properties):
java
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
- Controller 映射
/:
java
@RequestMapping("/")
public String welcome() {
return "index"; // 跳转到 WEB-INF/jsp/index.jsp
}
- Spring Cloud 微服务(网关层配置欢迎页)
如果是网关(如 Spring Cloud Gateway),需在网关服务中配置静态资源欢迎页:
-
将
index.html放在网关服务的static目录下 -
网关路由规则中放行
/路径(避免被路由到其他微服务)
总结表
| 框架 / 场景 | 配置方式 | 访问路径 |
|---|---|---|
| Spring Boot 静态欢迎页 | static/index.html 直接放置 |
http://localhost:8080/ |
| Spring Boot 动态欢迎页 | Controller 映射 / + templates/index.html |
http://localhost:8080/ |
| Spring MVC 静态欢迎页 | webapp/index.html + <mvc:default-servlet-handler/> |
http://localhost:8080/项目名/ |
| Spring MVC 动态欢迎页 | Controller 映射 / + 视图解析器配置 WEB-INF/views/index.jsp |
http://localhost:8080/项目名/ |
| SSM 整合欢迎页 | 同 Spring MVC 配置 | http://localhost:8080/项目名/ |
| Spring Boot + JSP 欢迎页 | webapp/WEB-INF/jsp/index.jsp + 视图解析器配置 + Controller 映射 / |
http://localhost:8080/ |