Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
Once you've set up your development and debug environment, you're ready to try building a sample "Hello World" application using Dojo. The simplest way to do this would be to set up an HTTP server, create a static HTML page, and use Dojo in that page. Alternately, you could create a dynamic HTML page using the technology of your choice, such as Java EE, PHP, or .NET. For this exercise we'll create a Java EE Web application.
Foundations of Ajax co-author Nate Shutta talks about JavaScript libraries, debuggers, and frameworks in a JavaWorld podcast with Andrew Glover. Listen now!
You can use your IDE of choice to create the HelloDojo Web application. Or you can use a text editor and a build script that allows you to package your code in a .war file. I developed
the sample code using the Eclipse IDE for Java EE Developers, which provides tools for creating dynamic Web applications and for deploying application code on the Apache Tomcat server
from within the IDE. It allows you to try out your changes rapidly.
Follow these steps to create the Hello World sample application:
workspaceroot/HelloDojo/WebContent/js folder.
index.html page (shown in Listing 1) in the workspaceroot/HelloDojo/WebContent folder.<html>
<head>
<title>Hello World</title>
<script type="text/JavaScript" src="js/dojo/dojo.js"
djConfig="parseOnLoad: true">
</script>
<style type="text/css">
@import "js/dijit/themes/tundra/tundra.css";
@import "js/dojo/resources/dojo.css";
</style>
<script type="text/JavaScript">
dojo.require("dijit.form.Button");
</script>
</head>
<body class="tundra">
<button dojoType="dijit.form.Button" id="helloButton">
Hello World
<script type="dojo/method" event="onClick">
alert("Hello Dojo.");
</script>
</button>
</body>
</html>
index.html file is a simple HTML document that generates one button using the dijit.form.button widget. When you click on that button the application displays "Hello Dojo" in an alert message. Lets take a closer look
at each of these elements:
dojo.js file is the main component in the Dojo toolkit. You can use Dojo by including this file in your page:<script type="text/JavaScript" src="js/dojo/dojo.js"
djConfig="parseOnLoad: true"> </script>
WebContent/js folder, so you must use the js/dojo/dojo.js path. The djConfig attribute is used for configuring the Dojo runtime, which I'll talk about more in the next section.
<style type="text/css">
@import "js/dijit/themes/tundra/tundra.css";
</style>
tundra.css in index.html.
dojo.require() call -- similar to Java's import statement -- is used for importing a Dojo module:<script type="text/JavaScript">
dojo.require("dijit.form.Button");
</script>
dojo.require("dijit.form.Button") tells Dojo to load the dijit.form.Button class on your page.
dojoType attribute indicating name of the Dijit widget that you want to use:<button dojoType="dijit.form.Button" id="helloButton">
Hello World
<script type="dojo/method" event="onClick">
alert("Hello Dojo.");
</script>
</button>
dojoType="dijit.form.Button" attribute to the HTML button element. Dojo also allows you to attach event handlers in a convenient way by declaring them inline. If you don't feel comfortable
with this convention you can declare a function in a script element and specify its name as a value of the onClick attribute.
web.xml file for your Web application to set index.html as the application's default page:<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HelloDojo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
HelloDojo Web application on your application server. If you use Eclipse and Apache Tomcat, then you can just right-click your HelloDojo project and select Execute on server to install the HelloDojo Web application on Tomcat.
http://localhost:8080/HelloDojo. You should get a page that looks like Figure 1 when you click on the Hello World button.HelloDojo is resulting in quite a few requests for additional resources such as .js, .css, and .html files. All the resources are requested
from the same location where dojo.js resides.