Im doing a sample application using dojo and json.
I have 2 classes:
1. Book class
package com.esolaria.do joex;
Expand|Select|Wrap|Line Numbers
- import org.json.JSONObject;
- import org.json.JSONException;
- public class Book {
- private int bookId;
- private String title;
- private String isbn;
- private String author;
- public Book(int bookId, String title, String isbn, String author) {
- this.bookId = bookId;
- this.title = title;
- this.isbn = isbn;
- this.author = author;
- }
- public void setBookId(int bookId) {
- this.bookId = bookId;
- }
- public int getBookId() {
- return this.bookId;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getTitle() {
- return this.title;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- public String getIsbn() {
- return this.isbn;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getAuthor() {
- return this.author;
- }
- public String toJSONString() throws JSONException {
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("bookId", new Integer(this.bookId));
- jsonObj.put("title", this.title);
- jsonObj.put("isbn", this.isbn);
- jsonObj.put("author", this.author);
- return jsonObj.toString();
- }
- }
Expand|Select|Wrap|Line Numbers
- package com.esolaria.dojoex;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.json.JSONObject;
- import org.json.JSONException;
- public class BookManager {
- private static List books = new ArrayList();
- static {
- books.add(new Book(1, "Crime and Punishment", "0679734503", "Fyodor Dostoevsky"));
- books.add(new Book(2, "The Collected Tales of Nikolai Gogol", "0375706151", "Nikolai Gogol"));
- books.add(new Book(3, "King Rat", "0440145465", "James Clavell"));
- books.add(new Book(4, "The Alchemist", "0062502182", "Paulo Coelho"));
- books.add(new Book(5, "A Tale of Two Cities", "0451526562", "Charles Dickens"));
- }
- public static Book getBook(int bookId) {
- Book returnValue = null;
- for (Iterator iter = books.iterator(); iter.hasNext();) {
- Book book = (Book) iter.next();
- if (book.getBookId() == bookId){
- returnValue = book;
- break;
- }
- }
- return returnValue;
- }
- public static List getAllBooks() {
- books.size();
- List book = BookManager.getBooks();
- for(Iterator item = book.iterator(); item.hasNext();){
- Book books = (Book) item.next();
- System.out.println(books.getBookId());
- System.out.println(books.getAuthor());
- System.out.println(books.getIsbn());
- System.out.println(books.getTitle());
- }
- return book;
- }
- public static List getBooks() {
- return books;
- }
- }
The middle layer is jsp: getAllBooks.jsp
Expand|Select|Wrap|Line Numbers
- <%@ page import="java.util.Iterator,
- java.util.List,
- com.esolaria.dojoex.Book,
- com.esolaria.dojoex.BookManager" %>
- <%
- List book = BookManager.getBooks();
- for(Iterator item = book.iterator(); item.hasNext();){
- Book books = (Book) item.next();
- try{
- out.println(books.toJSONString());
- }catch(Exception e){
- System.out.println(e);
- }
- }//for loop ends
- %>
My index file is having the following code:
[HTML]<%@ page
import="java.ut il.Iterator,
java.util.List,
com.esolaria.do joex.Book,
com.esolaria.do joex.BookManage r" %>
<% List books = BookManager.get Books();%>
<html>
<body>
<head>
<title>Exampl e 1</title>
<script language="javas cript" src="../js/dojo/dojo.js"></script>
<script language="javas cript">
dojo.require("d ojo.io.*");
dojo.require("d ojo.event.*");
dojo.require("d ojo.html.*");
function trMouseOver(boo kId) {
getBookInfo(boo kId);
}
function trMouseOut(evt) {
//var bookDiv = document.getEle mentById("bookI nfo");
//bookDiv.style.d isplay = "none";
}
function getBookInfo(boo kId) {
//var params = new Array();
//params['bookId'] = bookId;
var bindArgs = {
url: "../actions/getAllBooks.jsp ",
error: function(type, data, evt){alert("err or");},
mimetype: "text/plain"
//content: params
};
var req = dojo.io.bind(bi ndArgs);
dojo.event.conn ect(req, "load", this, "populateDi v");
}
function populateDiv(typ e, data, evt) {
var result = "";
for(var i = 0; i < 5; i++){
var result += data.bookId +","+ data.title +","+ data.isbn +","+ data.author;
}
alert(result);
}
</script>
</head>
<body>
<h1>Books</h1>
<p>
Hover over book title for more information.
</p>
<table border="1" cellspacing="1" cellpadding="3" style="backgrou nd-color:lavender; border: solid 1px #CCCCCC">
<% for (Iterator iter = books.iterator( ); iter.hasNext(); ) {
Book book = (Book) iter.next(); %>
<tr onmouseover="tr MouseOver(<%=bo ok.getBookId()% >)" onmouseout="trM ouseOut(<%=book .getBookId()%>) ">
<td><%=book.get Title()%></td><td></td>
</tr>
<% } %>
</table>
<div id="bookInfo" style="display: none;"></div>
</body>
</html>
[/HTML]
QUESTION:
Im getting the response as a list but wanted to know how to iterate through that list to get the information of each individual book in seperate row of the table.
Thanks in advance.