持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情
前言
大家好,我是掘金新用户小钻风头领,今天是我正式更文的第九天;
1.创建spring boot应用,选中需要的模块 2.spring boot就默认配置好了这些场景 3.编写业务代码
2,Spring Boot对与静态资源的映射规则
@ConfigurationProperties(prefix = "spring.resources",ignoreUnknownFields = false)
public class ResourceProperties {
//可以设置和静态资源有关的配置
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));
}
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));
}
}
}
1.) 所有的 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 下找资源;
静态资源都可以去:www.webjars.org/ 这个地方去下载依赖
<!--引入jQuery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
访问路径:http://localhost:8080/webjars/jquery/3.3.1/jquery.js
2.)/** 访问当前项目的任何资源,this.staticPathPattern = "/**"; 以下是静态资源的文件夹
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
};
"/" :代表项目的根路径
对应项目的路径位置
localhost:8080/xxx 如果没有处理 就回去静态资源路径下找
3.),欢迎页的配置
静态文件夹下的所有index.html都被 “/**”映射 ,满足localhost:8080 所以就会直接去静态文件夹下找index.html
3,模板引擎
spring boot选用Thymeleaf;
1.)引入
<!--引入Thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.)语法
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";
//规则 :只要将模板引擎放在 "classpath:/templates/" 下 Thymeleaf就能自动渲染
使用:
1.导入thymeleaf的命名空间
<html lag="en" xlmns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lag="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${hello }">这里是不经过Controller的默认信息</span>
</body>
</html>
2.使用语法:
Controller传过来信息 和springmvc一样即可
<span th:text="${hello }">这里是不经过Controller的默认信息</span>
3.)语法规则
th:text ; 改变当前元素的文本内容 th:任意HTML属性;来替换原生属性
... ...| 编号 | 属性 | 描述 | 示例 |
|---|---|---|---|
| 1 | th:text | 计算其值表达式并将结果设置为标签的标签体 | 中国 ,值为 null 为空时,整个标签不显示任何内容。 |
| 2 | th:utext | th:text 会对结果中的特殊字符转义,th:utext 不会转义 | 中国 ,值为 null 为空时,整个标签不显示任何内容。 |
| 3 | th:attr | 为标签中的任意属性设置,可以一次设置多个属性 | < a href="" th:attr="title='前往百度',href='baidu.com'"> 前往百度 |
| 4 | th:* | 为 html 指定的属性设值,一次设置一个 | < a href="" th:title='前往百度' th:href="'baidu.com'">前往百度< /a > |
| 5 | th:alt-title | 同时为 alt 与 title 属性赋值 | < a href="#" th:alt-title="'th:A-B'">th:A-B |
| 6 | th:lang-xmllang | 同时为 lang 、xmllang 属性赋值 | |
| 7 | th:fragment | 定义模板片段 | |
| 8 | th:insert | 将被引用的模板片段插⼊到自己的标签体中 | |
| 9 | th:replace | 将被引用的模板片段替换掉自己 | |
| 10 | th:include | 类似于 th:insert,⽽不是插⼊⽚段,它只插⼊此⽚段的内容 | |
| 11 | th:remove | 删除模板中的某些代码片段 | |
| 12 | th:each | 迭代数据,如 数组、List、Map 等 | |
| 13 | th:if | 条件为 true 时,显示模板⽚段,否则不显示 | 已婚1 |
| 14 | th:unless | 条件为 false 时,显示模板⽚段,否则不显示 | 已婚2 |
| 15 | th:switch | 与 Java 中的 switch 语句等效,有条件地显示匹配的内容 | |
| 16 | th:case | 配合 th:switch 使用 | 管理员 操作员 未知用户 |
| 17 | th:with | 定义局部变量 | |
| 18 | th:inline | 禁用内联表达式,内联js,内联css |
4,扩展SprinMVC
编写配置类 @Configuration , 并且是WebMvcConfigurationAdapter类型,
@Configuration
public class MyConfigMvc implements WebMvcConfigurer {
//映射视图 2.0以后的
@Bean
public WebMvcConfigurer webMvcConfigurer() {
WebMvcConfigurer configurer = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
}
};
return configurer;
}
}