Springboot---显示图片/字符串/map集合/list集合

 

 

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 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

结果:Springboot---显示图片/字符串/map集合/list集合_第1张图片

 

 

2.List集合

@GetMapping("/hello2")
    public String test2(Model model){
        List list=new ArrayList();
        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>indexth>
        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}">indextd>
        tr>
    table>
body>

Springboot---显示图片/字符串/map集合/list集合_第2张图片

 

你可能感兴趣的:(Springboot---显示图片/字符串/map集合/list集合)