Table of Contents Previous Section

Using WebScript in a WebObjects Application

This section discusses using WebScript in the context of a WebObjects application. For a detailed discussion of the structure of a WebObjects application, see the chapter "Getting Started."

The Role of Scripts in a WebObjects Application

In developing WebObjects applications, you usually write your business logic as compiled Objective-C code (though you can write entire applications using just WebScript). You then use WebScript to provide your "interface logic." A WebScript script typically includes the following ingredients:



Component Scripts

Most scripts are for components. A component is a page or a identifiable part of a page that can dynamically generate itself and send action messages when users interact with it. A component contains one or more dynamic HTML elements and usually some static HTML elements as well. Components can exist on the server or on the client browser. (See "Java Client-Side Components" for an example of the latter.)

On the server side, components are instances of a WOComponent subclass. (WOComponent is an abstract class that defines the interface and behavior of WOComponent objects.) At run time, WebObjects creates an instance of a special subclass for each component script and dynamically makes the script code the class implementation. Applications can, and often do, have multiple components.

In most cases, a script for a server-side component has a corresponding declarations file and HTML template. The declarations file provides a mapping between the actions and variables defined in the script, and the HTML elements that will be dynamically generated and then substituted in the HTML template.The three files in a group have the same base name but different extensions; for example, Main.wos (script), Main.wod (declarations), and Main.html (template). These application resources are used by the WOComponent object to prepare responses to user requests.

In a WebObjects application you generally put each group of three files (the script, the declaration, and the HTML template) into a directory that has the same base name and the extension .wo. So, for example, you can have a directory Main.wo that contains the files Main.wos, Main.wod, and Main.html. The script associated with a component (in this example, Main.wos) is called a component script.


Figure 1. The Contents of a Component Directory


The Application and Session Scripts

In addition to having one or more components, a WebScript application can also include an application script and a session script. The application script is where you declare and initialize application variables, and where you perform tasks that affect the entire application. The session script is where you declare, initialize, and store variables that persist throughout a session; in a session script you also perform tasks that affect the session as a whole. For more information on application, session, and other variables, see the section "Variables and Scope.."

The application script has the name Application.wos and the session script is named Session.wos. Both files reside immediately under the application (.woa) directory. Similar to component scripts, the script code is dynamically made the implementation code of special WOApplication (Application.wos) and WOSession (Session.wos) subclasses from which instances are generated at run time.

Table of Contents Next Section