【问题标题】:get multiple record in a list获取列表中的多条记录
【发布时间】:2013-10-04 06:52:30
【问题描述】:
Hii i am running a named query and in my table i have multiple notifications for a corresponding userid so i need to get these multiple notification in a list and then show on my jsp 
here is my code..
JAVA CODE
public ModelAndView  getNotifications(int id){

        System.out.println("Method ios called");
        Session session = usermanagementSessionFactory.openSession();

        System.out.println("after Getting Session");
        int  id1  = id;
        System.out.println(id1);
        Query query=(Query) session.getNamedQuery("getNotification");
        query.setInteger("userId",id1);

        List<Message> list1= query.list();

        ModelAndView mv = new ModelAndView("notifications");
        for(Message userData:list1)
        {

            String notifications=userData.getNotification();
            boolean isNew=(boolean) userData.isNew();

            ArrayList li = new ArrayList();
            li.add(notifications);


            //li.add(mname1);

        if(isNew)
        {

            mv.addObject("loginsuccess" ,li);

        }else
        {
            mv.addObject("loginsuccess" ,"There is no New Notifications");

        }


    }

        return mv;



}

JSP FILE

<h3 style="margin-top:100px;"> <core:forEach items="${loginsuccess}" var="items">
            <core:out value="${items}"></core:out>
</core:forEach>

它不工作只显示 tble 中的最后一个条目 sooplz 建议我需要做什么, 列表将如何获取所有记录 需要进行哪些更改 它不工作只显示 tble sooplz 中的最后一个条目建议我需要做什么, 列表将如何获取所有记录需要进行哪些更改

【问题讨论】:

    标签: spring hibernate model-view-controller named-query


    【解决方案1】:

    我认为在 for 循环中为每个通知创建一个新的 arrylist 对象并添加通知,以便它提供唯一的最后一个通知

    你可以试试这个

    public ModelAndView getNotifications(int id){

        System.out.println("Method ios called");
        Session session = usermanagementSessionFactory.openSession();
        ArrayList li =null;
        System.out.println("after Getting Session");
        int  id1  = id;
        System.out.println(id1);
        Query query=(Query) session.getNamedQuery("getNotification");
        query.setInteger("userId",id1);
    
        List<Message> list1= query.list();
        ArryList li = new ArrayList();
        ModelAndView mv = new ModelAndView("notifications");
        for(Message userData:list1)
        {
    
            String notifications=userData.getNotification();
            boolean isNew=(boolean) userData.isNew();
    
    
            li.add(notifications);
    
    
            //li.add(mname1);
    
        if(isNew)
        {
    
            mv.addObject("loginsuccess" ,li);
    
        }else
        {
            mv.addObject("loginsuccess" ,"There is no New Notifications");
    
        }
    
    
    }
    
        return mv;
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多