【问题标题】:How to link two objects in a jsp result?如何在jsp结果中链接两个对象?
【发布时间】: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):

【问题讨论】:

    标签: spring jsp


    【解决方案1】:

    问题是如何将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>
    

    【讨论】:

    • 完美修复。非常感谢。
    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多