thymeleaf list集合中的不同的map对象

一:

首先通过vo把不同的对象封装起来。

代码示例:

我的vo对象:

public class ViewObject {
	private Map<String, Object> objs = new HashMap<>();

	public void set(String key, Object value) {
		objs.put(key, value);
	}

	public Object get(String key) {
		return objs.get(key);
	}
}

Controller:

List<Blog> blogList = blogService.findAllBlog();
List<ViewObject> vos = new ArrayList<>(); 
for (Blog blog : blogList) {
	ViewObject vo = new ViewObject();
     User user = userService.getUserById(blog.getUserid());// 返回一个user对象
	logger.error(user.getName());
	vo.set("blog", blog);
	vo.set("user", user);
	vos.add(vo);
}
		
model.addAttribute("vos", vos);

html代码部分:

<div class="col-md-8" th:if="${not #lists.isEmpty(vos)}" >  <!-- 空值判断 -->
				 	
		<div th:each="blogs,userStat:${vos}">
				
		<li th:text="${blogs.get('blog').title}"></li>
		<li th:text="${blogs.get('user').name}"></li>
</div>

这样就可以把list里面的map对象一个个都迭代出来了。。。。注意下userStat是个状态变量。

写得应该很清楚了。

参考:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

 

二:vo中存放的是list集合的遍历方法

例子:

controller:

List<Comment> comments = commentService.getAllCommentByblogId(blogId);	
ViewObject vos = new ViewObject();
vos.set("user", user);
vos.set("blog", blog);
vos.set("comment", commentsAndUser);

html部分:

<div class="card-block" id="mainContainer"
            th:if="${not #lists.isEmpty(vos)}">

	<div class="row"  th:each="com,userStat:${vos.get('comment')}" >												
		<span  th:text="${com.id}"></span>
	</div>
</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值