I have to be able to store data in a new object, that I am getting from a complex object where each item is held in a List.
The hierarchy is the following:
1) libraryId (Library)
2) bookId (book)
3) pageId (page)
4) wordId (word)
- A library can have zero to many books. Each book is unique and can have zero to many pageId’s, if there is a page, each pageId can have zero to many wordId’s.
I have the data, but I need to cycle through each item and put into an object – ArrayList or similar – so I can pass off to another class, which is expecting a List. In the next class, the data is to be presented in a JSP data table.
I have the following classed to do this:
class Library{
int libraryId;
ArrayList<Book> books;
}
class Book {
int bookId;
ArrayList<Page> pages;
}
class Page {
int pageId;
ArrayList<Word> words;
}
class Word {
int wordId;
}
I have started to do it like this:
Book book = new Book(); // create Book object
ArrayList<Page> pages = new ArrayList<Page>;
pages.add(new Page()); // add first page
pages.add(new Page()); // add next page
book.setPages(pages); // set reference to pages for book
I have tried this.I am not sure I have it working correctly. This is how i have done it, based on the above answer. I the the same classes etc.
I have the following data example:
1 Book, which has 1 page, that page has 8 words. I have another book, which has 2 pages, each page has 6 words. I hope this makes sense.
Based on the above example, how to do I write this code, and how to I then retrieve the data back out too? The aim is that I pass this to a JSP, then retieve this out into a table using JSTL . Any help with this, to retrieve this data would also be helpful.
This really depends on your framework. Are you are a glutton for punishment and using straight JSP, if so, take a look at <jsp:useBean> or use a scriptlet (ugh…) to populate the “book” variable.
Otherwise, it will be very framework dependent. Something like spring would be: