1、Thymeleaf的字面量
1.1 什么是thymeleaf的字面量
字面量:对应数据类型的合法取值,可以在 html 页面直接使用,不需要后台传递
1.2 创建module
1.3 编写pojo-User类
package com.zzy.springboot.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
}
1.4 编写LiteralUserController
package com.zzy.springboot.web;
import com.zzy.springboot.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LiteralUserController {
/**
* thymeleaf字面量
* @param model
* @return
*/
@RequestMapping(value = "/literal")
public String literal(Model model){
model.addAttribute("sex", "man");
model.addAttribute("data", "字面量");
model.addAttribute("flug", true);
User user = new User();
user.setId(100);
user.setName("张三");
User user1 = new User();
model.addAttribute("user", user);
model.addAttribute("user1", user1);
return "literal";
}
}
1.5 在src/main/resources/temolate/下创建literal.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
1.6 字面量定义
- 文本字面量
用单引号’…'包围的字符串为文本字面量
- 数字字面量
- 布尔字面量
- null字面量
1.7 编辑literal.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>