Spring boot 与Web 开发 学习笔记
使用 Spring Boot
- 创建Spring Boot 应用,选中我们需要的模块
- Spring Boot 已经默认将这些场景配置好啦
只需要在配置文件中 指定少量的配置则项目就可以运行起来。
3)自己编写业务代码;
自动配置原理?
这个场景 Spring Boot 帮我们配置了 什么 , 怎么进行配置的,能不能修改? 能修改那些配置?能不能扩展 ??
xxxAutoConfiguration: 帮我们给容器中自动配置组件
xxxProperties : 配置类来封装配置文件的内容;
Spring Boot 对静态资源的映射规则;
@Deprecated
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties extends Resources {
// 可以设置和静态资源有关的参数
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
}
}
}
private Integer getSeconds(Duration cachePeriod) {
return cachePeriod != null ? (int)cachePeriod.getSeconds() : null;
}
private void customizeResourceHandlerRegistration(ResourceHandlerRegistration registration) {
if (this.resourceHandlerRegistrationCustomizer != null) {
this.resourceHandlerRegistrationCustomizer.customize(registration);
}
}
添加资源关系映射:
- 所有/webjars/, 都去classpath:/META-INF/resources/webjars/ 找资源
webjars 以jar 包的方式引入静态资源。
参考
2)“/” 访问当前项目的任何资源
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/" : 当前项目的根路径
localhost:8080/abc == 去静态资源文件夹中找abc
静态资源的位置的情况
3) 欢迎页 ;静态资源文件夹下所有的 index.html 页面; 被“/**” 映射;
localhsot:8080/ 找index 页面
- 所有的 **/favicon.ico 都是在静态文件夹都是在静态资源文件下找;
3 模板引擎
JSP Velocity Freemarker Thymeleaf;
Spring Boot 推荐的Thymeleaf; 模板引擎
高级语言的模板引擎、 语法简单 功能强大。
Thymeleaf 语法
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
只要我们把HTML 页面放在"classpath:/templates/",thymeleaf 就能自动渲染。
- 导入thymeleaf 的名称空间
xmlns:th="http://www.thymeleaf.org"
使用thtmeleaf 的语法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>成功!</h1>
<!-- th:text 将doiv 里面的文本内容设置为-->
<div th:text="${hello}">
这是显示欢迎信息
</div>
</body>
</html>
thymeleaf 的语法规则
1)th:text 改变当前的元素里面的文本内容
th 可以任意替换 html 中所有 属性的值
属性的优先级
表达式语法
参考的第四章节
https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.pdf

本文概述了如何使用SpringBoot创建应用,自动配置原理,静态资源映射,并重点介绍了Thymeleaf模板引擎的使用。通过实例讲解了Thymeleaf的配置和基本语法,适合Web开发者深入理解SpringBoot开发实践。

1945

被折叠的 条评论
为什么被折叠?



