springboot初步2

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

  • xxxxAutoConfiguraion:向容器中自动配置组件
  • xxxxProperties:自动配置类,装配配置文件中自定义的一些内容

一、访问静态资源

总结:

1. 在springboot, 我们可以使用以下方式处理静态资源

○ webjars localhost:8080/webjars/

○ public, static, /**, resources localhost:8080/


  1. 本地静态资源目录优先级

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>

  1. webapp 目录下放置 index.html(路径:webapp/index.html

  2. spring-mvc.xml 中开启默认 Servlet 处理静态资源:

java 复制代码
<!-- 开启静态资源默认处理(让 Tomcat 处理静态资源) -->
<mvc:default-servlet-handler/>
  1. 访问:http://localhost:8080/项目上下文路径/

方式 2:通过 Controller 跳转欢迎页

  1. 编写 Controller 映射 / 路径:
java 复制代码
@Controller
public class WelcomeController {
    @RequestMapping("/")
    public String welcome() {
        return "index"; // 跳转到 WEB-INF/views/index.jsp(需配置视图解析器)
    }
}
  1. 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>
  1. 要求:WEB-INF/views/index.jsp 存在。

3.其他常用场景

  1. SSM 整合(Spring + Spring MVC + MyBatis)

同 Spring MVC 的配置方式,需注意:

  • 静态资源欢迎页:webapp/index.html + <mvc:default-servlet-handler/>

  • 动态欢迎页:Controller 映射 / + 视图解析器配置

  1. Spring Boot 整合 JSP 的欢迎页

Spring Boot 不推荐 JSP,但如需使用:

  1. webapp/WEB-INF/jsp/ 下放置 index.jsp

  2. 配置视图解析器(application.properties):

java 复制代码
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
  1. Controller 映射 /
java 复制代码
@RequestMapping("/")
public String welcome() {
    return "index"; // 跳转到 WEB-INF/jsp/index.jsp
}
  1. 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/
相关推荐
独自破碎E2 小时前
【纵向扫描】最长公共前缀
java·开发语言
pp起床2 小时前
【苍穹外卖】Day03 菜品管理
java·数据库·mybatis
IT空门:门主2 小时前
Spring AI Alibaba使用教程
java·人工智能·spring
yaoxin5211232 小时前
303. Java Stream API - 查找元素
java·windows·python
weixin_462446232 小时前
Linux/Mac 一键自动配置 JAVA_HOME 环境变量(含 JDK 完整性校验)
java·linux·macos
虾说羊2 小时前
JWT的使用方法
java·开发语言
xzl042 小时前
小智服务端chat入口工具调用流程
java·服务器·前端
CTO Plus技术服务中2 小时前
2026版Java web高并发面试题和参考答案
java·jvm·spring·spring cloud·面试·tomcat·java-consul
2301_803554522 小时前
Qt中connect()实现信号与槽连接这一核心机制
java·数据库·qt