1.字符串/图片/map集合
@GetMapping("/hello") public String test(Model model){ String message="first thymeleaf !!"; model.addAttribute("message",message); User u = new User(); u.setId(1); u.setName("ttttttt"); u.setAge(18); Map<String,Object> map=new HashMap<>(); map.put("s1","/static/1.jpg"); map.put("s2","/static/2.jpg"); model.addAttribute("message", message); model.addAttribute("user", u); model.addAttribute("src", map); return "index2"; }
index2.html<body> <h1 th:text="${message}"></h1> <img th:src="${src.s1}"/></br> <img th:src="${src.s2}"/></br> 实体类信息<br> <span th:text="${user.id}"></span> <span th:text="${user.name}"></span> <span th:text="${user.age}"></span> <br> </body>
访问:http://localhost:8080/test/hello
结果:
2.List集合
@GetMapping("/hello2") public String test2(Model model){ List<User> list=new ArrayList<User>(); User u1 = new User(); u1.setId(1); u1.setName("11111111"); u1.setAge(18); list.add(u1); User u2 = new User(); u2.setId(2); u2.setName("2222222222"); u2.setAge(28); list.add(u2); User u3 = new User(); u3.setId(3); u3.setName("3333333333"); u3.setAge(88); list.add(u3); User u4 = new User(); u4.setId(4); u4.setName("4444444444"); u4.setAge(888); list.add(u4); model.addAttribute("userList", list); return "index3"; }
<body> <table width="200" style="text-align: center;"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>index</th> </tr> <tr th:each="user,iterStat : ${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td th:text="${iterStat.index}">index</td> </tr> </table> </body>
来源:https://www.cnblogs.com/crazy-lc/p/12258864.html