目录
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>thymeleaf入门title>
- head>
- <body>
- <h2 th:text="${msg}">你好h2>
- body>
- html>
4.template中的html文件不能直接访问,需要编写Controller跳转到页面中
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.GetMapping;
-
- @Controller
- public class PageController {
- //页面跳转
- @GetMapping("/show")
- public String showPage(Model model){
- model.addAttribute("msg","Hello Thymeleaf");
- return "index";
- }
- }
5.进行springboot配置
配置日志格式:
- #日志格式
- logging.pattern.console=%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n
Thymeleaf文件后缀是html,因为它本质上就是一个html文件。
1.准备模型数据
用上面那个PageController类即可。
2.在视图展示model中的值
3.启动项目,访问localhost:8080/show
可以看到输入框里有了初始值。
启动项目,访问localhost:8080/show
操作时间的内置对象为dates
1.准备数据
(130,01,01)的意思是从1900年往后数130年,即2030年,1月1号
model.addAttribute("date",new Date(130,01,01));
2.使用内置对象操作时间
- <span th:text="${#dates.format(date)}">span> <hr/>
- <span th:text="${#dates.format(date,'yyyy/MM/dd')}">span> <hr/>
- <span th:text="${#dates.year(date)}">span>
- <span th:text="${#dates.month(date)}">span>
- <span th:text="${#dates.day(date)}">span>
1.准备数据
model.addAttribute("sex","女");
2.进行条件判断
- <div>
- <span th:if="${sex} == '男'">性别:男span>
- <span th:if="${sex} == '女'">性别:女span>
- div>
1.准备数据
model.addAttribute("id","12");
2.进行条件判断
- <div th:switch="${id}">
- <span th:case="1">ID为1span>
- <span th:case="2">ID为2span>
- <span th:case="3">ID为3span>
- <span th:case="*">ID为*span>
- div>
- public class Users {
- private String id;
- private String name;
- private int age;
- // 省略getter/setter/构造方法
- }
2.准备数据
- List
users = new ArrayList<>(); - users.add(new Users("1","Jack",23));
- users.add(new Users("2","Tom",22));
- users.add(new Users("3","admin",25));
- model.addAttribute("users",users);
3.在页面中展示数据
- <table border="1" width="50%">
- <tr>
- <th>IDth>
- <th>Nameth>
- <th>Ageth>
- tr>
-
- <tr th:each="user : ${users}">
- <td th:text="${user.id}">td>
- <td th:text="${user.name}">td>
- <td th:text="${user.age}">td>
- tr>
- table>
- <tr th:each="user,status : ${users}">
- <td th:text="${user.id}">td>
- <td th:text="${user.name}">td>
- <td th:text="${user.age}">td>
- <td th:text="${status.index}">td>
- <td th:text="${status.count}">td>
- <td th:text="${status.size}">td>
- <td th:text="${status.odd}">td>
- <td th:text="${status.even}">td>
- <td th:text="${status.first}">td>
- <td th:text="${status.last}">td>
- tr>
1.准备数据
- Map
map = new HashMap<>(); - map.put("user1",new Users("1","shangxuetang",23));
- map.put("user2",new Users("2","baizhan",22));
- map.put("user3",new Users("3","admin",25));
- model.addAttribute("map",map);
2.遍历map
- <table border="1" width="50%">
- <tr>
- <th>IDth>
- <th>Nameth>
- <th>Ageth>
- <th>Keyth>
- tr>
-
- <tr th:each="m : ${map}">
- <td th:text="${m.value.id}">td>
- <td th:text="${m.value.name}">td>
- <td th:text="${m.value.age}">td>
- <td th:text="${m.key}">td>
- tr>
- table>
- request.setAttribute("req","HttpServletRequest");
- session.setAttribute("ses","HttpSession");
- session.getServletContext().setAttribute("app","application");
2.获取域数据
- <span th:text="${#request.getAttribute('req')}"/ >
- <span th:text="${#httpServletRequest.getAttribute('req')}"/>
- <hr/>
-
-
- <span th:text="${session.ses}"/>
- <span th:text="${#httpSession.getAttribute('ses')}"/>
- <hr/>
-
- <span th:text="${application.app}"/>
- <span th:text="${#servletContext.getAttribute('app')}"/>
<a th:href="@{http://www.baidu.com}">百度a>
- model.addAttribute("id","100");
- model.addAttribute("name","Tom");
- @GetMapping("/show2")
- @ResponseBody
- public String show2(String id,String name) {
- return id+":"+name;
- }
3.在URL中添加参数
index.html中添加
- <a th:href="@{show2?id=1&name=Tom}">静态参数一a>
- <a th:href="@{show2(id=2,name=Kobe)}">静态参数二a>
- <a th:href="@{'show2?id='+${id}+'&name='+${name}}">动态参数一a>
- <a th:href="@{show2(id=${id},name=${name})}">动态参数二a>
访问localost:8080/show
1.准备跳转后访问的Controller
- @GetMapping("/show3/{id}/{name}")
- @ResponseBody
- public String show3(@PathVariable String id,@PathVariable String name){
- return id+":"+name;
- }
2.在URL中添加参数
<a th:href="@{/show3/{id}/{name}(id=${id},name=${name})}">restful格式传递参数方式a>
在Springboot配置文件中可以进行Thymeleaf相关配置
示例:
比如在application.yml中配置
- spring:
- thymeleaf:
- prefix: classpath:/templates/
- suffix: .html
- encoding: UTF-8
- cache: false
- servlet:
- content-type: text/html
不过Thymeleaf一般不需要配置,默认配置就挺好。