[DOJO]JSON+JAVA+Netbeans 예제

참고 :
http://www.javapassion.com/handsonlabs/ajaxdojojson/     <<<구버젼 DOJO 수정요함
http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/ajax-transports/passing-data-json
<<DOJO 신버젼 문서 참고


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page
    import="java.util.Iterator,
    java.util.List,
    com.esolaria.dojoex.Book,
    com.esolaria.dojoex.BookManager" %>
<%--
    Document   : index
    Created on : 2008. 4. 9, 오후 10:58:39
    Author     : emillekorea
--%>

<%
            List books = BookManager.getBooks();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Dojo and JSON Example</title>
       
        <!-- SECTION 1 -->
        <style type="text/css">
            @import "http://o.aolcdn.com/dojo/1.0.0/dijit/themes/tundra/tundra.css";
            @import "http://o.aolcdn.com/dojo/1.0.0/dojo/resources/dojo.css";
        </style>
        <script type="text/javascript"
                djConfig="parseOnLoad: true"
                src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>          
       
        <!-- SECTION 2 -->
        <script type="text/javascript">
            // Load Dojo's code relating to the Button widget
            dojo.require("dijit.form.Button");
        </script>
        <script>
            // 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;
               
                dojo.xhrGet({
                    url: "actions/book.jsp",
                    load: populateDiv,
                    handleAs: "json",
                    error: helloError,
                    content: params
                });
            }
           
            // Function call to populate the "bookInfo" div element that is
            // defined at the end of this file.
            function populateDiv(data,ioArgs) {
                var bookDiv = document.getElementById("bookInfo");
                if (!data) {
                    bookDiv.style.display = "none";
                } else {
                bookDiv.innerHTML = "ISBN: " + data.isbn + "<br/>Author: " + data.author ;
                bookDiv.style.display = "";
            }
        }
       
        function helloError(data, ioArgs) {
            alert('Error when retrieving data from the server!');
        }
        </script>
    </head>
   
    <body class="tundra">
    <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>


-------------------------------------------
<%@ page import="java.util.Iterator,
         java.util.List,
         com.esolaria.dojoex.Book,
         com.esolaria.dojoex.BookManager" %>
action/book.jsp

<%
    String bookIdStr = request.getParameter("bookId");
    int bookId = (bookIdStr == null || "".equals(bookIdStr.trim()))
        ? 0 : Integer.parseInt(bookIdStr);
    Book book = BookManager.getBook(bookId);
    if (book != null) {
        out.println(book.toJSONString());
        System.out.println("itis: " + book.toJSONString());
    }
%>
-----------------------------------------------

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) {
        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"));
        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 getBooks() {
        return books;
    }
   
}
------------------------------------------------------------------------

by 키트 | 2008/04/10 14:57 | Ajax | 트랙백 | 덧글(0)
트랙백 주소 : http://hellokit.egloos.com/tb/1599956
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

<< 이전 페이지 다음 페이지 >>