Springboot请求方式
一.静态
1.存放位置
默认存放在 src/main/resources/static/ 目录下。
2.访问方式
直接通过URL路径访问,例如
文件:src/main/resources/static/page1.html
访问路径:
localhost:8080/page1.html
3.特点
- 内容固定
- 直接由服务器返回
二.动态
1.实现方式
使用Thymeleaf实现
1.1存放位置
存放在 src/main/resources/templates/ 目录下
1.2配置
在 pom.xml 中添加 Thymeleaf 依赖:
点击查看代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
点击查看代码
@Controller
@RequestMapping("/page")
public class PageController {
@RequestMapping(value ="/page2", method = {RequestMethod.GET, RequestMethod.POST})
public String page2(@RequestBody(required = false) User user, Map<String, Object> map){
map.put("name", "张三");
map.put("age", 17);
map.put("sex", false);
List<String> namelist = List.of("lll", "fff", "rrr");
map.put("namelist", namelist);
if (user != null) {
Map<String, String> dataMap = new ConcurrentHashMap<>();
dataMap.put("username", user.getUsername());
dataMap.put("password", user.getPassword());
map.put("dataMap", dataMap);
}
return "page2";
}
}
点击查看代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello page2 aaa<br>
<div th:text="'你好' + ${name}"></div>
<div th:if="${sex}">男</div>
<div th:unless="${sex}">女</div>
<div th:each="str:${namelist}">
<span th:text="${str}"></span>
</div>
<div th:text="${dataMap.username}"></div><br>
<div th:text="${dataMap.password}"></div><br>
</body>
</html>
| 语法 | 功能 | 示例 |
|---|---|---|
| th:text | 设置元素的文本内容 | |
| th:if | 条件判断(条件为真时显示 | 男 |
| th:unless | 条件判断(条件为假时显示 | 女 |
| th:each | 循环遍历 | ... |
浙公网安备 33010602011771号