Connecting a Dojo Tree to an ArrayList using JSON

Web 2.0 features are becoming increasingly predominant in web applications. Many web applications now use JavaScript toolkits such as Dojo, which allow web pages to behave more like desktop interfaces while overcoming browser incompatibilities and utilizing code that is maintainable, accessible, and standards-compliant.

This tutorial has been adapted from the Java One Hands-On Lab: Leveraging JavaScript Toolkits for End-to-End Connectivity in Web Applications, and demonstrates how to add and configure a Dojo Tree widget in a web page and enable the server-side to respond to Tree requests in JSON format. In doing so, you will utilize a set of freely available Java classes from to process data from an ArrayList into JSON format.

Content on this page applies to NetBeans IDE 7.2, 7.3, 7.4 and 8.0


To complete this document, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE 7.2, 7.3, 7.4, 8.0, Java EE
Java Development Kit (JDK) 7 or 8
GlassFish server
Tomcat servlet container
Open Source Edition 3.1.x or 4.x
7.x or 8.x
Dojo Toolkit version 1.8.x or later
Dojo sample project n/a


  • You will require an Internet connection to complete several of the steps included in this tutorial.
  • The NetBeans IDE Java EE download bundle enables you to optionally install and register the GlassFish Server Open Source Edition with the IDE. You require a server to simulate client-server communication in this tutorial.
  • The completed project looks as follows:
    Completed exercise viewed in browser

Opening the Sample Project

Begin by opening the sample project in the IDE. When the project is open, expand the project node in the Projects window and examine the Dojo resources and libraries that are used in the project.

  1. Download the tutorial's Dojo sample project to a location on your computer.
  2. Click the Open Project button ( Open Project button ) in the IDE toolbar to open the Open Project dialog box.
  3. In the Open Project dialog, locate the Dojo sample project on your computer and click Open Project.

    When you open the DojoTreeSample project in the IDE you will see that the project is badged with an error badge to indicate that a reference problem exists.

    Projects window displaying DojoTreeSample project in red text and with error badge

    The reference problem exists because the Java classes used in the project (Tribe, and TribeDataManager) reference classes found in the JSON JAR file, which you will later add in the section Adding the JSON JAR Files to the Project.

    Note: The Projects window (Ctrl-1; ⌘-1 on Mac) provides a logical view of important project contents, and is the main entry point to your project sources. The Files window (Ctrl-2; ⌘-2 on Mac) shows a directory-based view of your projects, and includes any files and folders that are not displayed in the Projects Window.

  4. Expand the Web Pages node in the Projects window.

    You can see that a resources folder is listed under the Web Pages node in the Projects window. The resources folder contains the Dojo core and Dijit libraries from the Dojo toolkit. In order to implement Dojo's Tree widget, you essentially require two components: the ItemFileReadStore module from the core library and the Tree widget itself contained in the Dijit library.

    • reads the JSON structured contents from an HTTP endpoint (in this tutorial, a servlet) and stores all the items in-memory for simple and quick access.
    • dijit.Tree: The Tree widget that provides a view of the JSON data retrieved from ItemFileReadStore.

    Note. The DojoX library is not required for this project.

  5. Expand the Libraries node in the Projects window and confirm that all the required libraries are on the classpath.

    Note. Depending on your configuration, you might need to resolve a missing server problem. If you see a <Missing Java EE Server> node under the Libraries node, right-click the project node and choose Resolve Missing Server Problem in the popup menu.

    Projects window displaying Dojo resources

    Select the GlassFish Server in the Resolve References dialog box. Click OK.

    Projects window displaying Dojo resources

At this stage, you have successfully opened the DojoTreeSample project in the IDE and confirmed that the Dojo libraries are included in the application. In the next step, you will begin working in the HTML file that will display the Tree widget to the end user.

Linking to the Toolkit Resources from a Project File

In order to use resources from the toolkit, you need to link to the dojo.js file, found in the core library. The dojo.js file is the source loader for Dojo and determines the correct host environment to use. While doing so, you can also configure djConfig by adding the parseOnLoad parameter.

  1. In the Projects window, double-click the dojoDemo.html file to open it in the editor.
  2. In the dojoDemo.html file, add the following <script> tags (in bold) between the <head> tags.
    <!-- TODO: link to Dojo resources here -->
    <script type="text/javascript">
        var djConfig = {parseOnLoad: true,
            isDebug: true};
    • djConfig allows you to override global settings that control how Dojo operates (e.g., using the parseOnLoad property).
    • parseOnLoad set to true ensures that widgets and page mark-up are parsed as the page is loaded.
  3. Add a link to the nihilo sample theme contained in the toolkit by adding the following @import statement (in bold) between the <head> tags and beneath the <script> tags that you added.
    <script type="text/javascript">
        var djConfig = {parseOnLoad: true,
            isDebug: true};
    <style type="text/css">
        @import "resources/dijit/themes/nihilo/nihilo.css";

    The nihilo theme is included by default in the toolkit. You can expand the dijit/themes folder in the Projects window to see other sample themes that are provided by default.

  4. Add the following class selector to the <body> tag of the page to specify the name of the theme you are using. When you do this, any Dojo widget which has been loaded into the page will be rendered using the styles associated with the theme.
    <body class="nihilo">

At this stage, the dojoDemo.html file is ready to accept any code that references the Dojo core and Dijit libraries, and will render any widgets using Dojo's nihilo theme.

Adding and Configuring the Dojo Tree Widget

After you have linked to dojo.js, you can begin adding code to utilize Dojo's modules and widgets. First add code to load the dijit.Tree widget and using dojo.require statements. Then, add the widget and module themselves to the page.

  1. Add the following dojo.require statements (in bold) to the file between the <body< tags.
    <script type="text/javascript">
        // TODO: add dojo.require statements here
  2. Add the following code (in bold) to add an ItemFileReadStore and Tree widget.
    <!-- TODO: specify AJAX retrieval -->
    <!-- TODO: add Tree widget and configure attributes -->
    <div dojoType=""
    <div dojoType="dijit.Tree"
         label="North American Indians">
    • ItemFileReadStore requires you to specify the url property by pointing to the server-side resource that returns the JSON data. As will be later demonstrated, this is the TribeServlet. You can use the jsId property to give the retrieved JSON data an id, which widgets can then use to refer to the data store.
    • Tree uses the store property to point to the ItemFileReadStore, which provides the JSON data. The query property enables you to arrange the display of data, based on a keyword used in the JSON file.

    Note. You can ignore the warnings that appear in the editor after adding this code.

At this stage, your dojoDemo.html file is complete, and all client-side modifications to the project are in place. In the following two steps, you'll make changes that affect the project's server-side behavior when Tree requests are made.

Adding Third-Party JSON Conversion Sources as a JAR File to the Project

In this tutorial, the logic that extracts the ArrayList sample data has been prepared for you in the Tribe and TribeDataManager classes. Essentially, it is only necessary to include the third-party Java classes that handle JSON conversion to the project, then add import statements for these classes in the Tribe and TribeDataManager classes. To accomplish this however, you need to first compile the third-party Java classes and create a Java Archive (JAR file). The IDE can help you do this using the Java Class Library wizard.

  1. Visit and note that Java classes for JSON conversion are freely available. Click the 'Free source code is available' link to download the file that contains the sources.
  2. Unzip the file and note that the extracted folder contains the sources listed on

    At this point, you want to compile these sources and create a Java archive (JAR file) which you will add to the DojoTreeSample project.

  3. Click the New Project button ( New Project button ) in the toolbar to open the New Project wizard.
  4. In the New Project wizard, select the Java Class Library project template in the Java category. Click Next.
  5. In the Name and Location panel of the Java Class Library wizard, type json as the Project Name. Click Finish.

    When you click Finish the new project is created and opens in the Projects window.

    You now need to copy the JSON sources that you download to the json project in the same way that you copied the Dojo toolkit resources to the DojoTreeSample project.

  6. Extract the archive and copy (Ctrl-C; ⌘-C on Mac) the Java source files that are in the root folder.

    Note. You do not need to copy the zip folder and its contents that is also located in the root folder of the extracted archive.

  7. In the IDE's Projects window, right-click the Source Packages node and choose New > Java Package in the popup menu.
  8. Type json as the Package Name. Click Finish.
  9. Right-click the json source package and choose Paste in the popup menu.

    When you expand the package you can see the json sources.

    Projects window - sources contained in 'json' project
  10. Right-click the json project node in the Projects window and choose Clean and Build to build the project.

    When you build your project, all Java classes get compiled into .class files. The IDE creates a build folder to contain compiled classes, as well as a dist folder that contains a JAR file for the project. These folders can be viewed from the IDE's Files window.

    After you build the json project, open the Files window (Ctrl-2; ⌘-2 on Mac) and expand the json folder. The build folder contains the compiled sources from the file and the dist folder contains the JAR file which the DojoTreeSample project needs to reference.

    Files window - compiled sources shown in 'build' folder

    Now that you have the json.jar file, you can resolve the reference problems that the DojoTreeSample project has been exhibiting since you opened it.

  11. In the Projects window, right-click the DojoTreeSample's Libraries node and choose Add JAR/Folder. Then, in the dialog, navigate to the location of the json project's dist folder and select the json.jar file.

    Alternatively, you can right-click the Libraries node and choose Add Project in the popup menu and locate the json project in the Add Project dialog box.

    When you exit the dialog, the json.jar file is listed under the project's Libraries node.

    Projects window - JAR file added to Libraries node

    Note: Although the json.jar file is listed under the project's Libraries node, it is referenced from its original location - not copied and added to the project (e.g., you won't be able to locate it under the DojoTreeSample project in the Files window). Therefore, if you change the location of the JAR file, the reference will be broken.

  12. Expand the Source Packages > dojo.indians package and double-click the Tribe and TribeDataManager classes to open them in the editor.
  13. Add necessary import statements to both classes. In each class, right-click in the editor and choose Fix Imports.

    The Tribe class requires the following imports:
    The TribeDataManager class requires the following imports:

    Note that the APIs for JSON classes are also provided at - you may want to keep this page open as you later examine code in Tribe and TribeDataManager.

  14. Examine the ArrayList in TribeDataManager. The ArrayList is a collection of Tribe objects. Looking at the first element of the ArrayList, you can see a new Tribe object created and added to the list:
    indians.add(new Tribe("Eskimo-Aleut", "Arctic", "Alaska Natives"));
    Each Tribe object captures three points of information: tribe, category, and region. The data for this exercise has been taken from Wikipedia's entry on Native Americans in the United States. As you can determine, multiple tribes are classified within a category, and numerous categories may be contained within a larger region.
  15. Open the Tribe class in the editor, and note that it is basically a JavaBean, with the exception of the toJSONObject() method:
    public JSONObject toJSONObject() throws JSONException {
        JSONObject jo = new JSONObject();
        jo.put("type", "tribe");
        return jo;
  16. Switch back to TribeDataManager (Ctrl-Tab) and examine the methods included in the class. Open the Navigator (Ctrl-7; ⌘-7 on Mac) to view a list of fields and properties contained in the class.
    TribeDataManager class viewed in the Navigator
    The most significant method contained therein is getIndiansAsJSONObject(). This method scans the ArrayList, processes the data, and returns it in the form of a JSONObject. The String form of the JSONObject is what is required by Dojo's ItemFileReadStore.
    public static JSONObject getIndiansAsJSONObject() throws JSONException {
        JSONObject jo = new JSONObject();
        JSONArray itemsArray = new JSONArray();
        jo.put("identifier", "name");
        jo.put("label", "name");
        // add regions
        // add categories
        // add tribes
        jo.put("items", itemsArray);
        return jo;
  17. Open the Javadoc on the getIndiansAsJSONObject() method. You can do this by returning to the Navigator (Ctrl-7; ⌘-7 on Mac) and hovering over the method. Otherwise, choose Window > Other > Javadoc from the main menu, then click on the method signature in the editor.
    Javadoc window opened to TribeDataManager class
  18. Examine the example of JSON data that is provided in the Javadoc. Note that the format of the data conforms to the examples provided in the Dojo documentation.

Within this step, you've compiled third-party sources from and added them as a JAR file to the DojoTreeSample project. You then added import statements to classes from the JAR file in the Tribe and TribeDataManager classes. Finally, you examined some of the methods contained in TribeDataManager which are used to convert the ArrayList data into a JSON string.

In the next step, you'll create a servlet which will handle incoming requests by calling the TribeDataManager's getIndiansAsJSONObject() method, and send the resulting JSON string a response to the client.

Preparing a Servlet to Initiate a JSON Response

Recall that you specified 'TribeServlet' as the value for the url property when adding the ItemFileReadStore to your web page. This is the destination on the server-side that is tasked with preparing and returning the JSON data to the client. Let's now create this servlet.

  1. In the Projects window, right-click the dojo.indians source package and choose New > Servlet.
  2. In the New Servlet wizard, type TribeServlet for the class name. Confirm that dojo.indians is specified as the package. Click Next.
    New servlet wizard
  3. Confirm that the default Servlet Name and URL Patterns values are correct. Click Finish to generate the skeleton class for the servlet.

    The function of the servlet is to call the getIndiansAsJSONObject() method, and use the data from this method to respond to the client request. In order to prepare a response in JSON format, we have to first set the mime type of the response to JSON format.

    Note. The wizard will automatically add the servlet name and URL pattern to web.xml. Consequently, any requests to the host domain (i.e., http://localhost:8080/DojoTreeSample/) for TribeServlet will be handled by the dojo.indians.TribeServlet class. If you open web.xml in the editor you can see that the file now contains the <servlet> and <servlet-mapping> elements.

  4. Modify the processRequest() method by making the following changes (in bold).

    This change sets the Content-Type header of the HTTP Response to indicate that any returned content is in JSON format.

  5. Replace the commented code within the processRequest() method's try block with the following (changes in bold):
    try {
        JSONObject jo = null;
        try {
            jo = TribeDataManager.getIndiansAsJSONObject();
        } catch (JSONException ex) {
            System.out.println("Unable to get JSONObject: " + ex.getMessage());
    } finally {

    To reformat your code, right-click within the editor and choose Format.

  6. Use the IDE's hints to add the following import statements.
  7. To run the project, select the DojoTreeSample project node in the Projects window, then click the Run Project ( Run Main Project button ) button in the IDE's toolbar.

    The browser opens to display the welcome page (dojoDemo.html), and you can see that the Dojo Tree widget is displaying data from the ArrayList properly, as in the screenshot above.

See Also

For more information about Dojo, refer to the official documentation:

For more information about JavaScript and JavaScript toolkit features on, see the following resources:

get support for the NetBeans


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo