Reputation: 2174
i have a class their i am getting book list based on its 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.
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
But My problem is : When i am trying to add book into the list , it only adds 1 book into the list. When user pass another book id to add into the arraylist then it is not inserting to list with previous one. My list size is showing always 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.
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;
}
}
Upvotes: 1
Views: 1624
Reputation: 376
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. 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.
Upvotes: 2