【SpringBoot】②WebMVC,Thymeleaf

实际2020-04-24 14:31:13
此篇文章使用版本:2.2.2.RELEASE
Thymeleaf 教程—易学文档

WebMvc自动配置

WebMvcAutoConfiguration【视图解析器前后缀+添加静态资源路径+欢迎页】分析
在这里插入图片描述

引入Webjars

引入Webjars的jQuery+访问路径映射关系+自动配置前后缀原理
在这里插入图片描述

设置favicon.ico

【我是使用的SpringBoot 2.2.2版本,我试了文件名为favicon.ico不生效,后面加个数字就生效了。】

1
2
<link rel="shortcut icon" href="favicon1.ico"/>
<link rel="bookmark" href="favicon1.ico"/>

在这里插入图片描述

扩展SpringMVC

实现 + 分析【容器中所有的WebMvcConfigurer都会一起起作用;我们的配置类也会被调用;】
在这里插入图片描述

全面接管SpringMVC

实现 + 分析【@EnableWebMvc注解】
在这里插入图片描述

拦截器

在这里插入图片描述

嵌入式容器

三种方式修改配置

在这里插入图片描述

配置原理分析

在这里插入图片描述

注册Servlet三大组件

SpringBoot注册Servlet三大组件【Servlet + Filter + Listener】
对应内容,可参考JavaWeb小总结
在这里插入图片描述

使用其他嵌入式容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<!-- 引入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>

<!--引入其他的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
</dependencies>

在这里插入图片描述

外部化启动

SpringBoot打war包,放入Tomcat启动

1)、修改POM,先改为war,可以自定义打包名称
在这里插入图片描述

2)、新建Controller进行测试,编写一个类继承SpringBootServletInitializer,重写configure方法
在这里插入图片描述

3)、使用Maven进行打包
在这里插入图片描述

4)、把target目录下的xxx.war放到Tomcat的webapps目录下,会自动解压
在这里插入图片描述

5)、默认端口号8080,虚拟根目录是解压后的文件夹名称
在这里插入图片描述

自定义Starter

1.0版本:https://img-blog.csdnimg.cn/20200705195310813.png
2.0版本:https://img-blog.csdnimg.cn/20200706101155344.png

1
2
3
4
5
6
7
8
9
10
2022-11-02 10:12:05 补,之前忘记记录了

# properties 配置文件提示
# xxx-spring-boot-starter 需要加入此依赖,打包会生成文件 META-INF/spring-configuration-metadata.json
# <optional>true</optional> 表示其他项目引入此starter,不会依赖 spring-boot-configuration-processor
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

3.0 增加了spring.factories

Thymeleaf

简单步骤

2021-02-27 13:50:28
1、在线文档
Thymeleaf 3.0 在线文档

2、导入依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3、关闭缓存

1
2
3
4
5
6
7
8
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html

# set to false for hot refresh
spring.thymeleaf.cache=false

4、在 @GetMapping 方法参数中加入 Model model,添加属性,页面则可直接引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
@GetMapping({"/", "index.html"})
public String getIndex(Model model) {
//获取所有的一级分类
List<CategoryEntity> catagories = categoryService.getLevel1Catagories();
model.addAttribute("catagories", catagories);
return "index";
}



<li th:each="catagory:${catagories}" >
<a href="#" class="header_main_left_a" ctg-data="3" th:attr="ctg-data=${catagory.catId}"><b th:text="${catagory.name}"></b></a>
</li>

入门案例

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<!--引入 thymeleaf 语法-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容替换 -->
<div th:text="${msg}">这是显示欢迎信息</div>
</body>
</html>

在这里插入图片描述

语法规则

1)、th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值
在这里插入图片描述

2)、表达式?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
表达式:
– #{...}:国际化消息
– ${…}:变量取值
*{…}:当前对象/变量取值
@{…}:url表达式
– ~{…}:片段引用
内置对象/共用对象:

判断/遍历:
th:if
th:unless
th:each
th:switch、th:case

th:属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Simple expressions:(表达式语法)

Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.

${session.foo}
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
补充:配合 th:object="${session.user}:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
@{/order/process(execId=${execId},execType='FAST')}
Fragment Expressions: ~{...}:片段引用表达式
<div th:insert="~{commons :: main}">...</div>

Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations:(布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _

实战

略…