- 第一:在Controller层创建HelloControl
package com.atguigu.springboot04.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 World";
}
@RequestMapping("/success")
public String success(Map<String, Object> map){
map.put("hello","<h1>你好</h1>");
map.put("users", Arrays.asList("zhangsan","lisi","wangwu"));
return "success";
}
}
- 第二:在resources/templates下创建success.html文件
- 第三:在success.html文件中使用thymeleaf语法获取"users"和“hello”中的值
<!DOCTYPE html>
<!--xmlns:th="http://www.thymeleaf.org"为代码提示-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功</h1>
<!--th:text 将div里面的文本内容设置为指定的值-->
<div th:text="${hello}"></div>
<hr/>
<!-- 转义 -->
<div th:text="${hello}"></div>
<!-- 不转义 -->
<div th:utext="${hello}"></div>
<hr/>
<!--每次遍历都会生成这个标签-->
<h4 th:text="${user}" th:each="user:${users}"></h4>
<hr/>
<h4>
<!--[[]]代表th:text-->
<span th:each="user:${users}">[[${user}}]]</span>
</h4>
</body>
</html>