简体   繁体   English

如何将对象一一添加到arraylist中以制作完整列表

[英]how to add object in a arraylist one by one in order to make a complete list

i have a class their i am getting book list based on its bookid.我有一个课程,他们根据 bookid 获取书籍列表。

Code for geting list:获取列表的代码:
Bookdetails book=new Bookdetails();book = dao.listBookDetailsById(Integer.parseInt(bookid));

After geting 1 book based on its Id, i am inserting the that book into a list and same list i am putting in session and returning it to my jsp page.根据其 ID 获得 1 本书后,我将该书插入到一个列表中,并将其放入会话中并将其返回到我的 jsp 页面。

Code for inserting book into list `将书插入列表的代码`

for(int i=1;i<=1;i++)
 {
    book = dao.listBookDetailsById(Integer.parseInt(bookid));    
    books.add(book);
 }
 session.put(BillTransactionBooksConstants.BOK, books );
 lists = (List) session.get( BillTransactionBooksConstants.BOK );
 System.out.println(lists.size()); `

Note: bookid is coming in my action class when each time users click a row in my jsp page注意:每次用户单击我的 jsp 页面中的一行时,bookid 都会出现在我的操作类中

But My problem is : When i am trying to add book into the list , it only adds 1 book into the list.但我的问题是:当我尝试将书添加到列表中时,它只将 1 本书添加到列表中。 When user pass another book id to add into the arraylist then it is not inserting to list with previous one.当用户传递另一本书 id 以添加到 arraylist 时,它不会插入到与前一个列表中。 My list size is showing always 1.我的列表大小始终显示为 1。

I want to insert all the books into the arraylist based on the id of those books when users passes bookid to my action class.当用户将 bookid 传递给我的操作类时,我想根据这些书籍的 id 将所有书籍插入到数组列表中。

Please help me to solve this issue.请帮我解决这个问题。

Full Action class code:完整的动作类代码:

import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    import v.esoft.actions.interceptor.VisionBooksConstants;
    import v.esoft.dao.BookdetailsDAO.BookdetailsDAO;
    import v.esoft.pojos.Bookdetails;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;

    public class AddBookToSession extends ActionSupport {
         Bookdetails book=new Bookdetails();
            List<Bookdetails> books = new ArrayList<Bookdetails>();
            List<Bookdetails> lists = new ArrayList<Bookdetails>();
            BookdetailsDAO dao = new BookdetailsDAO(); 
    Map session = ActionContext.getContext().getSession();

        @SuppressWarnings("unchecked")
        public String execute()
        { 
            HttpServletRequest request = ServletActionContext.getRequest();


            String bookid = request.getParameter("bid");  


            System.out.println("---------------Bookid-"+bookid);


            for(int i=1;i<=1;i++)
            {
              book = dao.listBookDetailsById(Integer.parseInt(bookid));   
              System.out.println("---------------Bookid-"+book); 
              books.add(book);
            }
                 session.put(BillTransactionBooksConstants.BOK, books );
                 lists = (List) session.get( BillTransactionBooksConstants.BOK );
                  System.out.println(lists.size()); 

            return SUCCESS;
        }

        public Bookdetails getBook() {
            return book;
        }

        public void setBook(Bookdetails book) {
            this.book = book;
        }

        public List<Bookdetails> getBooks() {
            return books;
        }

        public void setBooks(List<Bookdetails> books) {
            this.books = books;
        }

        public List<Bookdetails> getLists() {
            return lists;
        }

        public void setLists(List<Bookdetails> lists) {
            this.lists = lists;
        }   
    }

The problem is you are not loading your list from the session, so every time there is a new request you create a new list.问题是您没有从会话中加载您的列表,因此每次有新请求时,您都会创建一个新列表。

Edited: The first time you try to load the list from the session there is noting there, so we need to create the ArrayList.编辑:第一次尝试从会话加载列表时,那里没有注释,因此我们需要创建 ArrayList。 You can do this problably in some initialization routine but the following code presents a nice workaround.您可能可以在某些初始化例程中执行此操作,但以下代码提供了一个很好的解决方法。

Try this:尝试这个:

books = (List) session.get( BillTransactionBooksConstants.BOK );

if ( books == null ) books = new ArrayList<Bookdetails>();

book = dao.listBookDetailsById(Integer.parseInt(bookid));   
books.add(book);
session.put(BillTransactionBooksConstants.BOK, books );

Hope it works.希望它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM