狂神说:Springboot学习笔记(⼗)——Thymeleaf模板引擎⽂章⽬录
⼀、Thymeleaf
1.模板引擎
前端交给我们的页⾯,是html页⾯。如果是我们以前开发,我们需要把他们转成jsp页⾯,jsp好处就是当我们查出⼀些数据转发到JSP页⾯以后,我们可以⽤jsp轻松实现数据的显⽰,及交互等。
jsp⽀持⾮常强⼤的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项⽬⾸先是以jar的⽅式,不是war,像第⼆,我们⽤的还是嵌⼊式的Tomcat,所以呢,他现在默认是不⽀持jsp的。
那不⽀持jsp,如果我们直接⽤纯静态页⾯的⽅式,那给我们开发会带来⾮常⼤的⿇烦,那怎么办呢?
SpringBoot推荐你可以来使⽤模板引擎:
模板引擎,我们其实⼤家听到很多,其实jsp就是⼀个模板引擎,还有⽤的⽐较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有⾮常多,但再多的模板引擎,他们的思想都是⼀样的,什么样⼀个思想呢我们来看⼀下这张图:
模板引擎的作⽤就是我们来写⼀个页⾯模板,⽐如有些值呢,是动态的,我们写⼀些表达式。⽽这些值,从哪来呢,就是我们在后台封装⼀些数据。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终⽣成⼀个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。只不过呢,就是说不同模板引擎之间,他们可能这个语法有点不⼀样。其他的我就不介绍了,我主要来介绍⼀下SpringBoot给我们推荐的Thymeleaf模板引擎,这模板引擎呢,是⼀个⾼级语⾔的模板引擎,他的这个语法更简单。⽽且呢,功能更强⼤。
我们呢,就来看⼀下这个模板引擎,那既然要看这个模板引擎。⾸先,我们来看SpringBoot⾥边怎么⽤。
2.引⼊Thymeleaf
怎么引⼊呢,对于springboot来说,什么事情不都是⼀个start的事情嘛,我们去在项⽬中引⼊⼀下。给⼤家三个⽹址:
Spring官⽅⽂档:到我们对应的版本
到对应的pom依赖:可以适当点进源码看下本来的包!
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Maven会⾃动下载jar包,我们可以去看下下载的东西;
3.Thymeleaf分析
前⾯呢,我们已经引⼊了Thymeleaf,那这个要怎么使⽤呢?
我们⾸先得按照SpringBoot的⾃动配置原理看⼀下我们这个Thymeleaf的⾃动配置规则,在按照那个规则,我们进⾏使⽤。我们去⼀下Thymeleaf的⾃动配置类:ThymeleafProperties
@ConfigurationProperties(
prefix = "spring.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";
private String mode = "HTML";
private Charset encoding;
}
我们可以在其中看到默认的前缀和后缀!
我们只需要把我们的html页⾯放在类路径下的templates下,thymeleaf就可以帮我们⾃动渲染了。
使⽤thymeleaf什么都不需要配置,只需要将他放在指定的⽂件夹下即可!
⼆、测试
1.测试
1、编写⼀个TestController
@Controller
public class TestController {
@RequestMapping("/t1")
public String test1(){
//classpath:/templates/test.html
return "test";
}
}
2、编写⼀个测试页⾯  test.html 放在 templates ⽬录下
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>测试页⾯</h1>
</body>
</html>
3、启动项⽬请求测试
2.Thymeleaf 语法学习
要学习语法,还是参考官⽹⽂档最为准确,我们到对应的版本看⼀下;
我们做个最简单的练习 :我们需要查出⼀些数据,在页⾯中展⽰
1、修改测试请求,增加数据传输;
@RequestMapping("/t1")
public String test1(Model model){
//存⼊数据
model.addAttribute("msg","Hello,Thymeleaf");
//classpath:/templates/test.html
return "test";
}
2、我们要使⽤thymeleaf,需要在html⽂件中导⼊命名空间的约束,⽅便提⽰。我们可以去官⽅⽂档的#3中看⼀下命名空间拿来过来:
xmlns:th=""
3、我们去编写下前端页⾯
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<title>狂神说</title>
</head>
<body>
<h1>测试页⾯</h1>
<!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue⼀样-->
<div th:text="${msg}"></div>
</body>
</html>
4、启动测试!
3.研习Thymeleaf的使⽤语法
1、我们可以使⽤任意的 th:attr 来替换Html中原⽣属性的值!
2、我们能写哪些表达式呢?
Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调⽤⽅法
2)、使⽤内置的基本对象:#18
#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.
3)、内置的⼀些⼯具对象:
      #execInfo : information about the template being processed.
      #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.
      #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.
==================================================================================
Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是⼀样;
Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
Fragment Expressions: ~{...}:⽚段引⽤表达式
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)
springboot推荐算法
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _
4.练习测试
1、 我们编写⼀个Controller,放⼀些数据
@RequestMapping("/t2")
public String test2(Map<String,Object> map){
//存⼊数据
map.put("msg","<h1>Hello</h1>");
map.put("users", Arrays.asList("qinjiang","kuangshen"));
//classpath:/templates/test.html
return "test";
}
2、测试页⾯取出数据

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。