0
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>

it is not working shows only the last entry in the tble sooplz suggest me whats i need to do, how the list will get alll the record what changes are required it is not working shows only the last entry in the tble sooplz suggest me whats i need to do, how the list will get alll the record what changes are required

Nav Kumar
  • 141
  • 3
  • 5
  • 12

1 Answers1

0

I think in for loop for every notification create a new arrylist object and add the notification so its gives the only last notification

you can try this

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;



}
Chirag Kathiriya
  • 427
  • 5
  • 14