springboot

Thymeleaf基础 遍历List、Map、对象属性

2019-01-22  本文已影响1人  rainbowz
项目截图 图片.png

controller

package com.neo.web;

import com.neo.entity.People;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;


@RestController
public class Controller {
    @GetMapping("/")
    public People get() {
        return new People ("1", "socks", "123456", new Date(), "GMT");
    }

    private List<People> peopleList =new ArrayList<>();{
        peopleList.add( new People ("1", "socks1", "111111", new Date(), "GMT1"));
        peopleList.add( new People ("2", "socks2", "514730", new Date(), "GMT2"));
        peopleList.add( new People ("3", "socks3", "112233", new Date(), "GMT3"));
        peopleList.add( new People ("4", "socks4", "147892", new Date(), "GMT4"));
    }
    @GetMapping("z")
     public ModelAndView index(){
        return  new ModelAndView("user/user","userList",peopleList);
    }



    @GetMapping("z/{id}")
    public ModelAndView index(@PathVariable("id") int id){
        ModelAndView mv =new ModelAndView("user/user2");
        
        Map user= new HashMap();
        user.put("name", "姓名");
        user.put("age", "年龄");
        user.put("sex", "性别");
        user.put("birthday", "生日");
        user.put("phonenumber", "手机");
        System.out.println(peopleList.get(id));
       
        People p =new People("1", "socks", "123456", new Date(), "GMT");
        
        mv.addObject("p",p);
        mv.addObject("userList",peopleList);
        mv.addObject("map",user);
        mv.addObject("people",peopleList.get(id));
        mv.addObject("userList",peopleList);

        return  mv;

    }
}

user2.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>userList</title>
    <style type="text/css">
        .bodystyle{
            filter:alpha(opacity=100);
            -moz-opacity:1;
            -khtml-opacity: 1;
            opacity: 1;
            width:100%;
            position:absolute;

            background-attachment:fixed;
        }
    </style>


    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
    <script th:replace="head::static"></script>
</head>
<body class="container">
<div class="bodystyle">
    <div class="with:80%"  >
        <table class="table table-hover">
            <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>createTime</th>
                <th>timeZone</th>
                <th>password</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
            </thead>
            <tbody>
            <tr  th:each="user : ${userList}">
                <td th:text="${user.userId}">neo</td>
                <td th:text="${user.username}">neo</td>
                <td th:text="${user.createTime}">Otto</td>
                <td th:text="${user.timezone}">6</td>
                <td th:text="${user.password}">6</td>
                <td><a th:href="@{/toEdit(id=${user.userId})}">edit</a></td>
                <td><a th:href="@{/delete(id=${user.userId})}">delete</a></td>
            </tr>
            </tbody>
        </table>
    </div>
<!--
<div th:each="user:${userList}" >

    账号:<p  th:value="${user.userId}"></p>
    密码:<p th:value="${user.password}"></p>
    时间:<p th:value="${user.createTime}"></p>
  &lt;!&ndash;  时间:<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>&ndash;&gt;
</div>

账号:<p  th:value="${userList.userId}"></p>
密码:<p th:value="${userList.password}"></p>
时间:<p th:value="${userList.createTime}"></p>
-->
    <table>
            <thead>
           <th th:each="map:${map}" th:text="${map.key}" ></th>
           <th th:each="map:${map}" th:text="${map.value}"></th>  <!--遍历map集合-->
            </thead>
    </table>
<p th:each="people:${people}" th:text="${people}"></p>   <!--遍历list集合-->
<p th:text="${p.getPassword()}"></p>                        <!--输出people对象的password属性-->
</div>
</body>
</html>

图片.png

https://blog.csdn.net/u012485114/article/details/53906250

Thymeleaf 模板引擎中文文档:
https://blog.csdn.net/zhangcc233/article/details/80831056

https://www.jianshu.com/p/908b48b10702

上一篇下一篇

猜你喜欢

热点阅读