SpringBoot之模板引擎Thymeleaf的使用
SpringBoot不推荐使用JSP。
常见的模板引擎有:
JSP
Velocity
Thymeleaf
Freemarker
模板引擎的核心
SpringBoot推荐使用thymeleaf,引入依赖
<!--引入模板引擎 thymeleaf 现在默认引入的是2.x版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果想要引入3.x版本 ,需要再做设置
<properties>
<!--使用thymeleaf3.x版本-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!--布局功能的支持程序,thymeleaf3主程序 layout2以上版本 3-2 2-1-->
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
在源码中可以看到:
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
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 = "HTML5";
private Charset encoding;
//只需要将html放到classpath:templetas/,thymeleaf就能自动渲染
实例:
@RequestMapping("/index")
public String thymeleafTest(){
return "hello thymeleaf";
}
在resources目录下新建index.html,运行项目就能看到index.html的内容了。
在SpringBoot中如何使用thymeleaf
<!DOCTYPE html>
<!--导入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello</h1>
<!--th:text 将div里面的文本内容设置为指定的值-->
<div th:text="${name}">这是欢迎页面显示的内容</div>
</body>
</html>
如果想要看到controller方法返回的是页面而不是方法的返回值,注意不要再controller类上添加@RestController 而是添加@Controller
关于Thymeleaf的语法规则
- th:text 改变当前元素里面的文本内容
表达式
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.
- Selection Variable Expressions: *{…}
- Message Expressions: #{…}
- Link URL Expressions: @{…}
- 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)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
Special tokens:
- No-Operation: _
th:text和th:utext的区别:
th:text不会转义特殊字符,也就是说不会解析html标签,而th:utext反之
th:each作用的标签每次遍历都会产生新的标签