我有一个控制器可以从数据库中获取所有新闻,同时它会计算每条新闻的评论数量,但是我有一个问题,因为即使数据被正确地从数据库中拉出来也是如此,jsp不会将每个新闻对象与其对应的评论计数混合......
这是我的控制器:
@RequestMapping(value = "/viewstatus", method = RequestMethod.GET)
public ModelAndView viewStatus(ModelAndView modelAndView, @RequestParam(name = "p", defaultValue = "1") int pageNumber) {
Page<StatusUpdate> page = statusUpdateService.getPage(pageNumber);
for(StatusUpdate statusUpdate: page){
SiteUser siteUser= statusUpdate.getSiteUser();
modelAndView.getModel().put("siteuser", siteUser);
int countComments = commentService.countStatusComments(statusUpdate);
modelAndView.getModel().put("commentscounter", countComments);
}
modelAndView.getModel().put("page", page);
modelAndView.setViewName("app.viewStatus");
return modelAndView;
}
以下是控制台结果:
!!!!!! STATUSUPDATECONTROLLER:viewOneStatus:Count del comment:2 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 98,title = Title97,text = Status update 97,added = 2017-06-28 12:52:04.0,siteUser = SiteUser [id = 1,email = test@caveofprogramming.com,plainPassword = null,密码= $ 2a $ 10 $ TlfLjCcq8vPZHYkWcZ4rwurnqx5 / g5C.5nk3hGTdiG6 / cxlx1COPq,enabled = true,firstname = Mike,surname = River,repeatPassword = null,role = ROLE_ADMIN]] 2017-03-24 21:00:23.278 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL:选择count(comment0_.id)作为col_0_0_来自评论comment0_左外连接status_update statusupda1_ on comment0_。 statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:Count del comment:1 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 97,title = Title96,text = Status update 96,added = 2017-06-27 12:52:04.0,siteUser = SiteUser [id = 1,email = test@caveofprogramming.com,plainPassword = null,密码= $ 2a $ 10 $ TlfLjCcq8vPZHYkWcZ4rwurnqx5 / g5C.5nk3hGTdiG6 / cxlx1COPq,enabled = true,firstname = Mike,surname = River,repeatPassword = null,role = ROLE_ADMIN]] 2017-03-24 21:00:23.280 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL:选择count(comment0_.id)作为col_0_0_来自评论comment0_左外连接status_update statusupda1_ on comment0_。 statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:Count del comment:0 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 96,title = Title95,text = Status update 95,added = 2017-06-26 12:52:04.0,siteUser = SiteUser [id = 1,email = test@caveofprogramming.com,plainPassword = null,密码= $ 2a $ 10 $ TlfLjCcq8vPZHYkWcZ4rwurnqx5 / g5C.5nk3hGTdiG6 / cxlx1COPq,enabled = true,firstname = Mike,surname = River,repeatPassword = null,role = ROLE_ADMIN]] 2017-03-24 21:00:23.282 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_。 statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:Count del comment:0 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 95,title = Title94,text = Status update 94,added = 2017-06-25 12:52:04.0,siteUser = SiteUser [id = 1,email = test@caveofprogramming.com,plainPassword = null,密码= $ 2a $ 10 $ TlfLjCcq8vPZHYkWcZ4rwurnqx5 / g5C.5nk3hGTdiG6 / cxlx1COPq,enabled = true,firstname = Mike,surname = River,repeatPassword = null,role = ROLE_ADMIN]] 2017-03-24 21:00:23.284 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_。 statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select comment(comment0_.id)as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:Count del comment:0
这是JSP:
<table class="table table-hover">
<c:forEach var="statusUpdate" items="${page.content}">
<tr>
<td>
<ul class="list-inline posted-info">
<li>By
<a href="${contextRoot}/profile/${statusUpdate.siteUser.id}">
${statusUpdate.siteUser.firstname}
${statusUpdate.siteUser.surname}
</a>
</li>
<li>Posted</li>
<li>
<fmt:formatDate pattern="EEEE d MMMM y 'at' H:mm:ss" value="${statusUpdate.added}" />
</li>
</ul>
</td>
</tr>
<tr>
<td>
<h2>
<a href="${contextRoot}/viewonestatus/${statusUpdate.id}">${statusUpdate.title}></a>
</h2>
<p>${statusUpdate.text}</p>
</td>
</tr>
<tr>
<td>
<ul class="post-shares">
<li>
<a href="#"> <i class="rounded-x icon-speech"></i>
<span>${commentscounter}</span>
</a>
</li>
<li><a href="#"><i class="rounded-x icon-share"></i></a></li>
<li><a href="#"><i class="rounded-x icon-heart"></i></a></li>
</ul>
</td>
</tr>
</c:forEach>
</table>
结果出现了(所有评论都为0时):
答案 0 :(得分:0)
问题是如何将commentscouner
添加到模型中。
for(StatusUpdate statusUpdate: page){
SiteUser siteUser= statusUpdate.getSiteUser();
modelAndView.getModel().put("siteuser", siteUser);
int countComments = commentService.countStatusComments(statusUpdate);
// next line is a cause of problem
modelAndView.getModel().put("commentscounter", countComments);
}
每次使用相同名称设置新值时:commentscouner
。因此,每次重写上一次迭代循环时添加的值。这意味着当循环完成时,模型将只包含countComments
的最后一个值。如果最后一个值为0
,则JSP将为所有记录输出0
。
因此,您必须分别存储每个StatusUpdate
的评论数。例如在地图中:
HashMap<StatusUpdate, Integer> counterMap = new HashMap<StatusUpdate, Integer>();
for(StatusUpdate statusUpdate: page){
...
int countComments = commentService.countStatusComments(statusUpdate);
counterMap.put(statusUpdate, countComments);
}
modelAndView.put("counterMap", counterMap);
现在,在JSP中,您可以输出地图的值:
<table class="table table-hover">
<c:forEach var="statusUpdate" items="${page.content}">
...
<tr>
<td>
<ul class="post-shares">
<li>
<a href="#">
<i class="rounded-x icon-speech"></i>
<span>${counterMap[statusUpdate]}</span>
</a>
</li>
<li><a href="#"><i class="rounded-x icon-share"></i></a></li>
<li><a href="#"><i class="rounded-x icon-heart"></i></a></li>
</ul>
</td>
</tr>
</c:forEach>
</table>