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/
相关推荐
爬山算法19 分钟前
Hibernate(87)如何在安全测试中使用Hibernate?
java·后端·hibernate
云姜.32 分钟前
线程和进程的关系
java·linux·jvm
是码龙不是码农33 分钟前
支付防重复下单|5 种幂等性设计方案(从初级到架构级)
java·架构·幂等性
曹牧34 分钟前
Spring Boot:如何在Java Controller中处理POST请求?
java·开发语言
heartbeat..34 分钟前
JVM 性能调优流程实战:从开发规范到生产应急排查
java·运维·jvm·性能优化·设计规范
WeiXiao_Hyy37 分钟前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
苏渡苇43 分钟前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·设计模式·学习方法·责任链模式
团子的二进制世界1 小时前
G1垃圾收集器是如何工作的?
java·jvm·算法
long3161 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
独断万古他化1 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密