SpringBoot推荐使用Thymeleaf:语法简单,功能强大
字spring的官方文档
找到thymelead复制在pom.xml中进行导入
我们导入的是当前spring版本以来的对应的thymelead版本
<!--引入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
只要我们把html页面放置在classpath:/template/ 下,thymelead就会自动进行渲染
使用方法:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<div id="div01" class="myDiv" th:id="${kite}" th:class="${kite}" th:text="${kite}">
***${…}***:获取变量值;OGNL;(使用最多的获取值的方式)
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.
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).
4 . *{…}:选择表达式:和${}在功能上是一样;
5. #{…}:获取国际化内容
6. @{…}:定义URL;
其他的一些
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: _
我这里在success.html页面做了简单的演示
首先success.html的源码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h3>成功!</h3>
<div id="div01" class="myDiv" th:id="${kite}" th:class="${kite}" th:text="${kite}">这时显示欢迎信息</div>
<hr/>
<!--不转义特殊字符-->
<div th:text="${kite}"></div>
<!--转义特殊字符-->
<div th:utext="${kite}"></div>
<hr/>
<!--th:each每次都会生成当前的这个标签-->
<h4 th:text="${user}" th:each="user:${user}"></h4>
<hr/>
<h4>
<span th:each="user : ${user}">[[${user}]]</span>
</h4>
</body>
</html>
我们在controller类中添加了success方法
而且使用map添加了数据
package cn.kitey.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.Map;
@Controller
public class helloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello kite!";
}
@RequestMapping("/success")
public String success(Map<String,Object> map){
// classpath:/template/success.html
map.put("kite","hello kite"
);
map.put("user", Arrays.asList("张三","李四","王二"));
return "success";
}
}
也是就是SpringBoot中的Thymelead模板的简单介绍,主要的方法可以在thymelead官网的使用文档
thymelead使用文档
加油把。因为这里不能使用jsp页面才使用的thymelead模板