Contents
The goal of Project Phobos is to build a lightweight web application framework that runs on the Java platform but allows you to develop your entire application using a scripting language, such as JavaScript. As a result, you can take advantage of the many benefits that scripting languages offer but still leverage the power of the Java platform. When you develop with Phobos, you will experience the many benefits of using scripting languages. For example,
Phobos gives you what other scripting languages do not: access to the Java Platform, Enterprise Edition (Java EE) stack. As the first article in this series has pointed out, scripting languages and statically typed languages such as the Java programming language have their own strengths. When you use Phobos to create web applications, you can use scripting and Java technology in ways that take advantage of their strengths. And because Phobos runs on the Java EE platform, you can call into components of the Java EE stack. For example, from your Phobos application, you can call into the new and powerful Java Persistence API available as part of the Java EE platform. Phobos also simplifies development in Ajax, a technology that includes but is not limited to Asynchronous JavaScript and XML. If you have JavaScript on the client, as you do with Ajax, and on the server, as you can with Phobos, you get all all the benefits of having the same scripting language on both the client and the server. This also means that no translation is required between one language on the server and another on the client. In addition, Phobos includes a set of convenience libraries specifically for Ajax, such as the jMaki framework and the Dojo toolkit. This article first describes the Phobos architecture. It then uses a common calculator example to demonstrate the characteristics of a typical Phobos application. Next, it shows how to incorporate jMaki widgets into your Phobos application. Finally, it describes how to use the Java Persistence API with a Phobos application. Phobos Architecture
From the Phobos user's perspective, Phobos consists of a set of scripting engines and a set of built-in scripting libraries. The scripting engines are compliant with JSR 223, Scripting for the Java Platform, and each one allows you to use a particular scripting language to develop your application. Currently, Phobos supports the JavaScript and JRuby engines. The scripting libraries add convenience JavaScript functions for performing such common tasks as request dispatching and rendering views, as well as more specialized ones such as integrating Ajax functionality into your application. The application that you build with the Phobos framework adheres to a specific structure, which encourages building web applications according to the Model-View-Controller (MVC) design pattern. Every Phobos application consists of a set of controllers, a set of views, scripts for serving HTTP requests, and possibly other static content, such as style sheets and images. Once you have written your application, the flexible Phobos architecture allows you to run it on one of a variety of platforms and in a variety of environments. You can run a Phobos application on the open-source GlassFish server or in any compliant servlet container. Phobos also lets you select a particular environment configuration depending on whether the application is in the development, testing, or production stage. For example, you can enable or disable certain optimizations based on their appropriateness for the current stage of development. After you have deployed your application, it is live, meaning that you can make changes to the application while it is running and see the results instantaneously. There is no need to compile or redeploy the application. Let's take a closer look at what a Phobos application looks like. What Is a Phobos Application?
Every Phobos application includes an
As you can see from Figure 1, the application directory contains the following subdirectories:
More complicated applications might need additional directories,
such as a As Figure 1 shows, the calculator application contains the following files:
When the browser makes the first request for the page, the following happens:
The line in
This line uses the built-in The Controller Every Phobos application needs at least one controller, which processes requests, makes changes to the model in response to requests, and renders the view. In the case of the calculator, the responsibility of its controller is to render the view, perform the arithmetic operations of the calculator, and pass the results to the view.
The
Inside the calculator's
The
As shown in the preceding code, the value of the result is set to
zero because this is the first time the page is being requested. The
operation is set to a default of
The last thing the
Notice that all you need to do is provide the name of the file, as
long as you put the file in the appropriate directory. Phobos is smart enough to
know where to find the file. As a result, the
The extension
But first, let's go over what the
Now let's take a look at the view that is actually rendered. The View Every web application has a set of pages, or views. Figure 2 shows the view of the calculator application. The user enters numbers in the Current Total and Second Operand fields, selects an operator from the set of radio buttons, and clicks Compute. The application responds by displaying the total in the Current Total field.
When developing a Phobos application, you create a view using an embedded JavaScript file. This file is an HTML file with embedded JavaScript statements, and so it is similar to a JavaServer Pages (JSP) technology page with HTML form elements mixed with scripting tags. Following is the
Notice that when the user clicks the Compute button, the action of
the form submission is the As this section has shown, the architecture of a typical Phobos application conforms well to the familiar MVC design pattern. Using a jMaki Widget in a Phobos Application
When developing a Phobos web application, you can do anything that
you can do with a web application based on JSP technology, and that
includes adding Ajax capabilities to your application. Currently,
Phobos supports adding jMaki
widgets to a Phobos application. In the future, as Phobos and Dynamic
Faces mature, there might be more synergy between the two. In the meantime, this section shows you how you can use the jMaki fisheye widget in a Phobos application. Figure 3 showns a screen capture of the bioFisheyeWidget application, which you can download from this Phobos samples download page. This application allows a user to click on a photo to get biographical information about the person in the photo.
The first task you must perform to get Phobos and jMaki working
together is to download both projects and build them separately, as
described in "Building a Phobos Distribution." After that, to
include a jMaki widget into your application, just use the
The preceding code uses the To display the view with the biographical information, the view of the bioFisheyeWidget application includes a dcontainer widget that subscribes to a topic that gets the URL and will load it into the page:
This is just one example of how you can use the publish and subscribe feature of jMaki to respond to widget events. See the document "jMaki Glue" to learn how you can write your own topic listeners.
So that's how you include a prewrapped jMaki widget into your
application. To include a custom jMaki widget that is not part of the
jMaki distribution, you simply add the widget's Using the Java Persistence API in a Phobos Application
As this article has mentioned previously, one of the advantages of using Phobos is that you have access to all the great features that the Java EE platform offers. One of the latest ease-of-use features included in Java EE 5 is the Java Persistence API, which provides developers with an object-relational mapping facility for managing relational data in Java technology-based applications. The Java Persistence API allows you to represent an entity, which is typically a table in a relational database, with a Java class. The API also provides a set of classes for managing these entities:
See chapters 24 through 27 of the Java EE 5 Tutorial for more information on the Java Persistence API. Phobos provides a persistence JavaScript library that gives you the ability to access data from the views and scripts of your Phobos application using the Java Persistence API. The following simple code is from the jpaExample application, which you can download from this Phobos samples download page. The code shows part of a script file that adds authors to a database and then displays those authors on the page. It represents the simplest way to publish this information in a web page.
Figure 4 shows a screen capture of the page generated by this code:
As you can see from the code, the persistence library available with
Phobos includes a function to get an Conclusion
The Phobos web application framework gives you the flexibility of developing a web application with a scripting language but still gives the application access to everything that the Java EE platform provides. As Phobos matures, it will give you a lot more capabilities, including more sophisticated database access and support for REST web services. To keep up-to-date on the progress of Phobos, visit the Project Phobos web site and join the project aliases. For More Information
Previous articles in the "New Technologies for Ajax and Web Application Development" series:
JavaServer Pages (JSP) Technology Ajax Developer Resource Center Ajax Downloads JavaScript Technology Dojo Toolkit About the Author
Jennifer Ball is a staff writer at Sun Microsystems. She writes about web application technologies that are part of the Java EE platform. |
| ||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||