Home > Articles

  • Print
  • + Share This
From the author of

WebObjects Components

A component consists of an HTML file, a Java class file, and something called a WebObjects definition file. The HTML file provides all the static HTML elements for your component along with tags for the dynamic elements. Dynamic elements do not resolve themselves until runtime. In contrast, static HTML elements are exactly the same as when they were created by the developer. You can mix static and dynamic elements in a WebObjects component.

In this part of the chapter, we will start a new project to experiment with building components that contain dynamic elements. So set the Movies project aside for a while; we will get back to it in the next chapter.

Dynamic Elements

Dynamic elements contain certain attributes that are bound to Java variables and methods. When it comes time for these dynamic elements to resolve themselves, they use Java objects to determine how to display their content. A number of Dynamic Elements come with WebObjects; the more commonly used ones are displayed in Table 3.4.

Table 3.4 Common Dynamic Elements


The most common dynamic element, it represents dynamic text.


Dynamically displays its contents depending on a conditional value.


Repeats its contents dynamically.

There are also dynamic elements for forms, including text fields, lists, pop-up buttons, check boxes, and radio buttons. One of the biggest challenges of learning WebObjects is mastering the usage of all the dynamic elements. These will be covered throughout this book.


Dynamic elements contain attributes that govern how the dynamic element will behave, and every dynamic element has a different set of attributes. These attributes are "bound" to values that are relative to the component that contains the dynamic element. These bindings use key-value coding, which is described later, to resolve their values. One of the easiest ways to understand how these bindings work is to go through an example, and a good dynamic element to start with is the WOString.


The WOString displays dynamic text. The WOString is used to display the contents of one of your variables. It's almost the WebObjects equivalent to printf, only in HTML. The way a WOString works is that when the time comes for it to be displayed, it will display the contents of its value binding.

Using WOString, an Example

The following steps show how to use the WOString by building an application that displays a random number:

  1. Create a new WebObjects application project called RandomNumbers.

  2. Edit the Main.java file in Project Builder. This is the Java file for the Main component. Add a method called randomNumber() that returns a random integer between 0 and 100. The code for Main.java is in Listing 3.1.

  3. Listing 3.1

    The randomNumber() Simply Returns a Random Number between 0–100

    import com.apple.yellow.foundation.*;
    import com.apple.yellow.webobjects.*;
    import com.apple.yellow.eocontrol.*;
    import com.apple.yellow.eoaccess.*;
    public class Main
      extends WOComponent
      public int randomNumber()
        return (int)(Math.random()*100.0);
  4. Modify the Main component in WebObjects Builder. Type The random number is in the graphical display.

  5. With the cursor at the end of the sentence you just wrote, add a WOString by clicking the WOString icon.

  6. Now bind randomNumber to the WOString's value attribute. This is done by clicking randomNumber in the lower part of the WebObjects Builder window and dragging it to the center of the WOString as shown in Figure 3.19. If you do not hit the center of the WOString, a pop-up menu will appear with all the WOString attributes, and you can select the value attribute. Save your component.

  7. Figure 3.19 Click the randomNumber value in the Main component and drag it to the WOString to create a binding.

  8. Build and run your application from Project Builder. The result is shown in Figure 3.20.

  9. Figure 3.20 The finished random number application. Click the Refresh button to display a new number.

WOString Bindings

WOString does more than simply display text in an HTML component; the WOString can format your number or date, among other options. Table 3.5 contains a list of WOString bindings. The bindings for a particular dynamic element can be determined by inspecting the element. Figure 3.21 shows the inspector for a WOString. Documentation about the dynamic element can be displayed by clicking the Book icon in the inspector.

Figure 3.21 The Dynamic Inspector shows a list of the available bindings.

Table 3.5 WOString Attributes




If the value binding is to a NSGregorianDate object, the dateFormat will determine how the date is displayed. The available formats are documented in the NSGregorianDateFormatter class in the Foundation framework.


Bound to true (YES) or false (NO), this binding determines how the value will be displayed if it contains HTML control characters. If true, the HTML control characters will be escaped out, meaning that they will be explicitly displayed. If this is false, any HTML tags will be rendered normally.


If you are displaying data of a custom type, you can also specify a custom formatter. This must be a subclass of NSFormatter, located in the Foundation framework.


Similar to the dateFormat, this specifies how numbers are to be presented. The appropriate values are found in the documentation for the NSNumberFormatter class, located in the Foundation framework.


The value to be displayed. Non-string values are converted using the toString method.


Allows another value to be substituted when the value binding is null. This is useful when the WOString is placed in an HTML table. Instead of an empty table cell, you could set this binding to " ", which is HTML's non-breaking whitespace. This would need to be done in conjunction with the escapeHTML binding being set to NO.

In the WOString inspector, the dateFormat, numberFormat, and escapeHTML bindings all use pop-up lists to assist in setting values. The two format bindings have lists of common formats that allow you to get the formatting done a bit faster. The escapeHTML binding allows you to select YES or NO.

Bindings Behind the Scenes

These bindings that are being discussed are saved with the component. Every component has a directory containing up to three files: The HTML, WOD, and WOO files. The HTML file is the HTML template for the component, and contains place holders for the dynamic elements. The HTML file for the main component in the random numbers example is shown in Listing 3.2.

Listing 3.2


    <META name="generator" CONTENT="WebObjects 4.5">
    <FONT FACE="Courier, Fixed">The random number is:

The HTML file associated with a component consists of mostly legal HTML. Apple has added the WEBOBJECT tag used by WebObjects. The WEBOBJECT tag is found in Listing 3.2. There is always one attribute of this tag, the NAME. This NAME resolves to a name in the associated WOD file, also found in the component directory.

The WOD, or WebObjects definition file determines how the bindings relate to the Java code associated with the component. The WOD file contains a separate entry for every WEBOBJECT tag contained in the HTML file. The first line of the entry contains the name and type of the tag. For example, in Listing 3.2, the NAME specified in the WEBOBJECT tag was String1, which is also the name supplied on the first line of Listing 3.3. The WOD file also specifies that String1 is a WOString. The bindings are specified between the opening and closing braces, each with a key-value pair and ending with a semicolon.

Listing 3.3


String1: WOString {
  value = randomNumber;

Binding Resolution

You might have noticed that some bindings are surrounded by double quotes and some are not. For example, in the last tutorial, the value for the WOString was bound to randomNumber. A valid number format would be "###,##0". A value is resolved differently depending on whether it is in double quotes.

If a value is in double quotes, it is evaluated as a literal string. In the number format example, the value is always the string "###,##0".

If a value does not contain double quotes, it is resolved at runtime based on the contents of the Java code that is associated with the component. This resolution is done according to a process called key-value coding. The method valueForKey() is called with the key from the binding being passed in as an argument. The default for a WOComponent is to resolve this according to the key-value coding process, which means that the Java reflection API will be used to introspect the component, looking for certain methods or instance variables.

get Access Method

If the class has a non-private method with the same name as the binding with a prepended get, that method is called. The method must have no parameters and return something other than void. For example, a method with the signature public int getRandomNumber() would be called to resolve the randomNumber binding.

Java developers will recognize this naming convention as the appropriate convention for Java access methods.

Other Access Methods

The next point of search for the appropriate resolution is to look for a method that contains the same signature as the binding. For example, the method containing the signature public int randomNumber() would be used if getRandomNumber() was not found. This is the naming convention used by Objective-C, and is also supported by Java.


The access methods you provide must be non-private. If you provide a private method, the WebObjects framework will determine that it exists and then try to call it, resulting in a runtime error.

Instance Variable

If no method access method exists, the class is searched for a non-private instance variable with the same name as the binding.


The binding names must begin with a lowercase letter in order for that binding to be resolved properly, as is the convention for method names and instance variables.

A couple of other methods are searched with the same signature as the previous methods—with an underscore at the beginning. The order of the methods searched to resolve a binding is as follows:

  1. get access method; that is, getRandomNumber()

  2. access method; that is, randomNumber()

  3. get private access method; that is, _getRandomNumber()

  4. private access method; that is, _randomNumber()

  5. Instance variable, that is, randomNumber and then _randomNumber.

For more information about key-value coding, take a look at the EOKeyValueCoding interface, located in WOInfoCenter under Reference, Enterprise Objects Framework, EOControl Reference(Java), EOKeyValueCoding.

  • + Share This
  • 🔖 Save To Your Account