
This hands-on lab takes you through the basics of using JSON data
format along with Dojo Toolkit.
Expected duration: 60 minutes
This hands-on lab assumes you have some basic knowledge of, or programming experience with, the following technologies.
Before you begin, you need to install the following software on your
computer. The Dojo Toolkit 0.3.1 is already included in the
hands-on lab zip file so there is no need for you to download it
yourself.
In this exercise, you will learn how to
use JSON as a data format of choice between the
browser and the server. (This exercise was created based on the "Using
Dojo and JSON to Build Ajax Applications" article that was authored
by Zarar Siddiqi with the author's permission.)






| <%@ page import="java.util.Iterator, java.util.List, com.esolaria.dojoex.Book, com.esolaria.dojoex.BookManager" %> <% List books = BookManager.getBooks(); %> <html> <body> <head> <title>Dojo and JSON Example</title> <script language="javascript" src="dojo-0.3.1-ajax/dojo.js"></script> <script language="javascript"> dojo.require("dojo.io.*"); dojo.require("dojo.event.*"); dojo.require("dojo.html.*"); // Event handler when a user hovers a mouse over function trMouseOver(bookId) { getBookInfo(bookId); } // Event handler when a user hovers a mouse out function trMouseOut(evt) { var bookDiv = document.getElementById("bookInfo"); bookDiv.style.display = "none"; } // Invoked from trMoustOver(bookId) function call above function getBookInfo(bookId) { var params = new Array(); params['bookId'] = bookId; // Perform remote operation using JSON as data format // that will be returned from the server var bindArgs = { url: "actions/book.jsp", error: function(type, data, evt){alert("error");}, mimetype: "text/json", content: params }; var req = dojo.io.bind(bindArgs); // The "populateDiv" gets called as an event handler dojo.event.connect(req, "load", this, "populateDiv"); } // Function call to populate the "bookInfo" div element that is // defined at the end of this file. function populateDiv(type, data, evt) { var bookDiv = document.getElementById("bookInfo"); if (!data) { bookDiv.style.display = "none"; } else { bookDiv.innerHTML = "ISBN: " + data.isbn + "<br/>Author: " + data.author; bookDiv.style.display = ""; } } </script> </head> <body> <h1><center>Dojo and JSON Example - Move your mouse over the book titles!</center></h1> <table cellspacing="0" cellpadding="3" style="background-color:lavender; border: solid 1px #CCCCCC"> <!-- Display each book with Id and it title in a table format --> <% for (Iterator iter = books.iterator(); iter.hasNext();) { Book book = (Book) iter.next(); %> <!-- Whenever you hover your mouse over and out on a book title, trMouseOver() and trMouseOut event handlers get called --> <tr onmouseover="trMouseOver(<%=book.getBookId()%>)" onmouseout="trMouseOut(<%=book.getBookId()%>)"> <td><%=book.getTitle()%></td> </tr> <% } %> </table> <!-- This is the div element that will be populated in the populateDiv JavaScript function above, which req object is loaded --> <div id="bookInfo" style="display:none;"></div> </body> </html> |
|
// Invoked from trMoustOver(bookId) function call above function getBookInfo(bookId) { var params = new Array(); params['bookId'] = bookId; // Perform remote operation using JSON as data format // that will be returned from the server var bindArgs = { url: "actions/book.jsp", error: function(type, data, evt){alert("error");}, mimetype: "text/plain", content: params }; var req = dojo.io.bind(bindArgs); // The "populateDiv" gets called as an event handler dojo.event.connect(req, "load", this, "populateDiv"); } // Function call to populate the "bookInfo" div element that is // defined at the end of this file. function populateDiv(type, data, evt) { var bookDiv = document.getElementById("bookInfo"); if (!data) { bookDiv.style.display = "none"; } else { alert("Returned data from server in the JSON format but display in plain form = " + data); bookDiv.innerHTML = "ISBN: " + data.isbn + "<br/>Author: " + data.author; bookDiv.style.display = ""; } } |

| package com.esolaria.dojoex; /** * * @author sang */ public class Publisher { private String name; private String address; private int year; /** Creates a new instance of Publisher */ public Publisher() { } public Publisher(String name, String address, int year) { this.name = name; this.address = address; this.setYear(year); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } } |

| package com.esolaria.dojoex; import org.json.JSONObject; import org.json.JSONException; public class Book { private int bookId; private String title; private String isbn; private String author; private Publisher publisher; public Book(){ } public Book(int bookId, String title, String isbn, String author, Publisher publisher) { this.bookId = bookId; this.title = title; this.isbn = isbn; this.author = author; this.setPublisher(publisher); } 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); // Create a new JSONObject called jsonObj2 JSONObject jsonObj2 = new JSONObject(); jsonObj2.put("name", this.getPublisher().getName()); jsonObj2.put("address", this.getPublisher().getAddress()); jsonObj2.put("year", this.getPublisher().getYear()); // Add the newly created jsonObj2 to jsonObj jsonObj.put("publisher", jsonObj2); return jsonObj.toString(); } 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 Publisher getPublisher() { return publisher; } public void setPublisher(Publisher publisher) { this.publisher = publisher; } } |
| package com.esolaria.dojoex; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class BookManager { private static List books = new ArrayList(); // Initialize List of Book's' static { books.add(new Book(1, "Crime and Punishment", "0679734503", "Fyodor Dostoevsky", new Publisher("Google Publishing", "1 Google Land", 1900))); books.add(new Book(2, "The Collected Tales of Nikolai Gogol", "0375706151", "Nikolai Gogol", new Publisher("Yahoo Publishing", "1 Yahoo Land", 2002))); books.add(new Book(3, "King Rat", "0440145465", "James Clavell", new Publisher("Shin Company", "1 Never Land", 1899))); books.add(new Book(4, "The Alchemist", "0062502182", "Paulo Coelho", new Publisher("Daniel Company", "1 Boston", 1999))); books.add(new Book(5, "A Tale of Two Cities", "0451526562", "Charles Dickens", new Publisher("Young Publishing", "1 Dream Land",2001))); } 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 getBooks() { return books; } } |
| <%@ page import="java.util.Iterator, java.util.List, com.esolaria.dojoex.Book, com.esolaria.dojoex.BookManager" %> <% List books = BookManager.getBooks(); %> <html> <body> <head> <title>Dojo and JSON Example</title> <script language="javascript" src="dojo-0.3.1-ajax/dojo.js"></script> <script language="javascript"> dojo.require("dojo.io.*"); dojo.require("dojo.event.*"); dojo.require("dojo.html.*"); // Event handler when a user hovers a mouse over function trMouseOver(bookId) { getBookInfo(bookId); } // Event handler when a user hovers a mouse out function trMouseOut(evt) { var bookDiv = document.getElementById("bookInfo"); bookDiv.style.display = "none"; } // Invoked from trMoustOver(bookId) function call above function getBookInfo(bookId) { var params = new Array(); params['bookId'] = bookId; // Perform remote operation using JSON as data format // that will be returned from the server var bindArgs = { url: "actions/book.jsp", error: function(type, data, evt){alert("error");}, mimetype: "text/json", content: params }; var req = dojo.io.bind(bindArgs); // The "populateDiv" gets called as an event handler dojo.event.connect(req, "load", this, "populateDiv"); } // Function call to populate the "bookInfo" div element that is // defined at the end of this file. function populateDiv(type, data, evt) { var bookDiv = document.getElementById("bookInfo"); if (!data) { bookDiv.style.display = "none"; } else { bookDiv.innerHTML = "ISBN: " + data.isbn + "<br/>Author: " + data.author + "<br/>Publisher Name: " + data.publisher.name + "<br/>Publisher Address: " + data.publisher.address + "<br/>Publisher Established Year: " + data.publisher.year; bookDiv.style.display = ""; } } </script> </head> <body> <h1><center>Dojo and JSON Example - Move your mouse over the book titles!</center></h1> <table cellspacing="0" cellpadding="3" style="background-color:lavender; border: solid 1px #CCCCCC"> <!-- Display each book with Id and it title in a table format --> <% for (Iterator iter = books.iterator(); iter.hasNext();) { Book book = (Book) iter.next(); %> <!-- Whenever you hover your mouse over and out on a book title, trMouseOver() and trMouseOut event handlers get called --> <tr onmouseover="trMouseOver(<%=book.getBookId()%>)" onmouseout="trMouseOut(<%=book.getBookId()%>)"> <td><%=book.getTitle()%></td> </tr> <% } %> </table> <!-- This is the div element that will be populated in the populateDiv JavaScript function above, which req object is loaded --> <div id="bookInfo" style="display:none;"></div> </body> </html> |

| public class AuthorInformation
{ private String nationality; private int age; /** Creates a new instance of AuthorInformation */ public AuthorInformation() { } public String getNationality() { return nationality; } public void setNationality(String nationality) { this.nationality = nationality; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |