controller代码:
1 @RequestMapping("/success") 2 public String success(Map<String,Object> map){ 3 map.put("hello", "<h1>你好</h1>"); 4 map.put("users", Arrays.asList("张三","李四","王五")); 5 return "success"; 6 }
HTML代码:
1 <!DOCTYPE html> 2 <html lang="en" xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>success</title> 6 </head> 7 <body> 8 <h1>成功!!!</h1> 9 <div id="i1" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">这里显示欢迎信息</div> 10 <hr/> 11 <div th:text="${hello}"></div> 12 <div th:utext="${hello}"></div> 13 <hr/> 14 <!-- th:each每次遍历都会生成当前这个标签--> 15 <h3 th:text="${user}" th:each="user:${users}"></h3> 16 <h3> 17 <span th:text="${user}" th:each="user:${users}"></span> 18 </h3> 19 </body> 20 </html>
浏览器访问:loacalhost:8080/success