Thymeleaf基础 遍历List、Map、List「map」、Map「List」

@RequestMapping("/hello")
	public String hello(Model map){
        // 将要遍历的map
		Map user= new HashMap();
		user.put("name", "姓名");
		user.put("age", "年龄");
		user.put("sex", "性别");
		user.put("birthday", "生日");
		user.put("phonenumber", "手机");
		map.addAttribute("userhead", user);
        // 将要遍历的list
		List userinfo =new ArrayList();
		userinfo.add("周美玲");
		userinfo.add("32");
		userinfo.add("女");
		userinfo.add("1985");
		userinfo.add("19850014665");
		map.addAttribute("userinfo", userinfo);
        // 将要遍历的list<map>
		List outerList=new ArrayList<>();
		Map innerMap=new HashMap<>();
		for (int i = 0; i < 10; i++) {
			innerMap.put("1", i);
			innerMap.put("1", i++);
			i++;
			outerList.add(innerMap);
		}
		map.addAttribute("listmap", outerList);
		return "/hello";
	}
遍历Map,将会把上方的userhead渲染到table的头部;
遍历List,将会把userinfo渲染到table的body里。
<table class="table"> <thead> <th th:each="userhead : ${userhead}" th:text="${userhead.key}"></th> <th th:each="userhead : ${userhead}" th:text="${userhead.value}"></th> </thead> <tbody > <td th:each="userinfo:${userinfo}" th:text="${userinfo}"></td> </tbody> </table>
遍历List<Map>
<ul th:each="lm : ${listmap}"> <li th:each="entry : ${lm}" th:text="${entry.key}" ></li> <li th:each="entry : ${lm}" th:text="${entry.value}"></li> </ul>

遍历map里嵌套list

参考:

https://github.com/thymeleaf/thymeleaf/issues/748

@RequestMapping
    public ModelAndView show(HttpServletRequest req, HttpServletResponse resp, Model model){

        Map<String,Object> file = new HashMap<>();

        List<FileInfo> AA = Arrays
                .asList(new FileInfo("A1CLASS","A1ID","A1PATH","A1NAME"),
                        new FileInfo("A2CLASS","A2ID","A2PATH","A2NAME"),
                        new FileInfo("A3CLASS","A3ID","A3PATH","A3NAME"));

        List<FileInfo> BB = Arrays
                .asList(new FileInfo("B1CLASS","B1ID","B1PATH","B1NAME"),
                        new FileInfo("B2CLASS","B2ID","B2PATH","B2NAME"),
                        new FileInfo("B3CLASS","B3ID","B3PATH","B3NAME"));


        file.put("AA",AA);
        file.put("BB",BB);


        ModelAndView modelAndView = new ModelAndView("ia11/ia1101/ia1101");
        model.addAttribute("file",file);
        model.addAttribute("test","test");
        return modelAndView;
    }
<div th:each="mapEntry: ${file}">
  <div th:each="fileInfo: ${mapEntry.value}">
    <input type="text" class="form-control" readonly name="fileChoose_text" th:value="${fileInfo.fileName}">
    ...
  </div>
</div>

 

 

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值