【发布时间】:2017-03-24 20:35:31
【问题描述】:
我有一个控制器,它可以从数据库中获取所有新闻,同时它会计算每个新闻的 cmets 数量,但我遇到了一个问题,因为即使数据被正确地从数据库中提取出来, 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:计数删除评论: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 调试 1080 --- [nio-8080-exec-6] org.hibernate.SQL:从 cmets comment0_ 中选择 count(comment0_.id) 作为 col_0_0_ 离开外连接 status_update statusupda1_ on comment0_。 statusupdateid=statusupda1_.id 其中 statusupda1_.id=? Hibernate: select count(comment0_.id) as col_0_0_ from cmets comment0_ left external join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:计数删除评论: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 调试 1080 --- [nio-8080-exec-6] org.hibernate.SQL:从 cmets comment0_ 中选择 count(comment0_.id) 作为 col_0_0_ 离开外连接 status_update statusupda1_ on comment0_。 statusupdateid=statusupda1_.id 其中 statusupda1_.id=? Hibernate: select count(comment0_.id) as col_0_0_ from cmets comment0_ left external join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:计数删除评论: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 调试 1080 --- [nio-8080-exec-6] org.hibernate.SQL:从 cmets comment0_ 中选择 count(comment0_.id) 作为 col_0_0_ 离开外连接 status_update statusupda1_ on comment0_。 statusupdateid=statusupda1_.id 其中 statusupda1_.id=? Hibernate: select count(comment0_.id) as col_0_0_ from cmets comment0_ left external join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:计数删除评论: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 调试 1080 --- [nio-8080-exec-6] org.hibernate.SQL:从 cmets comment0_ 中选择 count(comment0_.id) 作为 col_0_0_ 离开外连接 status_update statusupda1_ on comment0_。 statusupdateid=statusupda1_.id 其中 statusupda1_.id=? Hibernate: select count(comment0_.id) as col_0_0_ from cmets comment0_ left external join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:计数删除评论: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>
结果来了(所有 cmets 不为 0 时为 0):
【问题讨论】: