babb
babb

Reputation: 423

How to cycle and store values into an ArrayList, whichj is to be passed to JSP for display

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)

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.

Upvotes: 0

Views: 1025

Answers (2)

Rahul Agrawal
Rahul Agrawal

Reputation: 8971

You can do like this

<c:forEach var="book" items="${bookList}">
                        <tr>                        
                            <td>${book.id}</td>
                            <td>${book.name}</td>
                        </tr>
                        <c:forEach var="page" items="${book.pages}">
                            <tr>                        
                                <td>${page.id}</td>
                                <td>${page.name}</td>
                            </tr>
                        </c:forEach>                    
                    </c:forEach>

Upvotes: 1

Matt
Matt

Reputation: 11805

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:

@Controller
public class MyController {
  @RequestMapping(...)
  public ModelAndView processRequest(HttpServletRequest request, 
        HttpServletResponse response) {
      Map<String, Object> model = new HashMap<String, Object>();
      Book book = ...;
      model.put("book", book);
      return new ModelAndView("viewbook", model);
  }
}

Upvotes: 1

Related Questions