我有一個控制器,可以從數據庫獲取所有新聞,並且同時計算每條新聞上的評論數量,但是我遇到了問題,因爲即使數據正確地從數據庫中提取出來, jsp的不每條新聞的對象與它對應的評論數混合...如何鏈接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 protected],plainPassword = null,password = $ 2a $ 10 $ TlfLjCcq8vPZHYkWcZ4rwurnqx5/g5C.5nk3hGTdiG6/cxlx1COPq,enabled = true,firstname = Mike,surname = River,repeatPassword = null,role = ROLE_ADMIN]] 2017-03-24 21:00:00 23.278 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL:select count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select count(comment0_.id)as col_0_0_ from 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 =狀態更新96,增加= 2017-06-27 12:52:04.0,siteUser = SiteUser [id = 1,email = [email protected],plainPassword = null,password = $ 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:select count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:計數刪除評論:0 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 96,title = Title95,text =狀態更新95,增加= 2017-06-26 12:52:04.0,siteUser = SiteUser [id = 1,[email protected],plainPassword = null,password = $ 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 count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? !!!!!! STATUSUPDATECONTROLLER:viewOneStatus:計數刪除評論:0 !!!!! VIEWSTATUS statusUpdate:StatusUpdate [id = 95,title = Title94,text =狀態更新94,增加= 2017-06-25 12:52:04.0,siteUser = SiteUser [id = 1,email = [email protected],plainPassword = null,password = $ 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 count(comment0_.id)as col_0_0_ from comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid = statusupda1_.id where statusupda1_.id =? Hibernate:select count(comment0_.id)as col_0_0_ from comment0_ left outer 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>
這裏來的結果(0所有的意見時,是不正確的):
完美修復。非常感謝。 – Mike
@Mike歡迎您 –