Home > Articles

WebObjects Components

📄 Contents

  1. Creating a New WebObjects Application
  2. WebObjects Components
  3. Using Images
  4. Summary
  5. Q&A
  • Print
  • + Share This
Using components speeds your development process, reduces the number of bugs, and makes life good in general. In WebObjects, the components represent dynamic pages of your application. You will be the component developer, and your tools will be WebObjects Builder and Project Builder. In this chapter, learn how to create a new WebObjects project using Project Builder, learn about WebObjects components, create a few WebObjects components, and use WOHyperlinks to navigate between WOComponents.
This sample chapter is excerpted from WebObjects Web Application Construction Kit.
From the author of

Component-ware, have you heard of it? It's supposed to replace OOP (object-oriented programming) as the software development paradigm of choice. The most popular components these days are JavaBeans, followed closely by Microsoft's COM and Enterprise JavaBeans. Using components speeds your development process, reduces the number of bugs, and in general makes life good, so the story goes. The potential of component-ware is to allow the developer to piece together an application without much (if any) coding using prebuilt components.

Components can be considered a little like LEGOs. You know how all the LEGO pieces magically fit together; it's the same with components. Now, you know the reason the LEGOs fit together so smoothly is that their pegs are just the right size to fit into the under side of the blocks. There's a specification somewhere in LEGOLAND that says what size the pegs and blocks should be. Because there's a specification for how blocks fit together, and because of the incredible price of LEGOs, a number of companies are creating LEGO knock offs that fit into LEGOs. The reason they fit is because they adhere to the same specification.

Components usually come with a specification, like LEGOs, and the also magically fit together like LEGOs. Just because everything fits together doesn't mean that what you're building will work, however. And, components are generally a little more difficult to fit together than LEGOs.

So, how are components different than objects? Well, components are objects that adhere to a certain specification. Components can also contain additional resources, such as text files that describe themselves. Objects are a lot more flexible than components because you really can do anything you want with objects. All components have to be accessible in a certain way, which gives the component developer a little less freedom.

In WebObjects, the components represent dynamic pages of your application. You will be the component developer, and your tools will be WebObjects Builder and Project Builder. Begin by starting your first WebObjects project.

In this chapter, you will

  • Create a new WebObjects project using Project Builder

  • Learn about WebObjects components

  • Create a few WebObjects components

  • Use WOHyperlinks to navigate between WOComponents

Creating a New WebObjects Application

This section will be a tutorial on creating your first WebObjects application. The application used here will display a random number when the main page refreshes. Start by creating a new WebObjects application using Project Builder.

Using Project Builder

Project Builder is found in the WebObjects program group under either the Start menu in Windows or the Apple menu on the Mac. When you initially start Project Builder, a project will not be loaded. On Windows, you will see the Loaded Projects window; on Mac, you will simply be presented with the Project Builder menu.

Creating a Project

To create a new project, select the Project, New menu items. You will be presented with the new project dialog box (see Figure 3.1 and Figure 3.2).

Figure 3.1 The Windows New Project dialog box.

Project Builder manages many different types of projects. The project type that you will use throughout this book is the WebObjects Application type.

Figure 3.2 Mac OS X New Project dialog box.

CAUTION

The WebObjects Application type is different from the Application project type, which is the default. If you choose the Application project type, you will not be able to create a WebObjects application.

In the New Project dialog box, you will need to enter the path for your new project. The name of the project is the last part of the path that you enter. For example, on Windows if you choose c:\Projects\WebObjects\Movies, the name of your project is Movies. Also if you choose that path, the Movies directory will be created in the c:\Projects\WebObjects directory. The other directories need to be there, however.

Go ahead and create a new WebObjects Application project called Movies. On the Mac, place it somewhere in your home directory. On Windows, place it in an appropriate directory. You will then be presented with the WebObjects Application Wizard, which is shown in Figure 3.3.

The WebObjects Application Wizard allows you to start your application with some pre-generated code. It also allows you to pick the language used in the application. Set the Available Assistance to None and the Primary Language to Java.

Figure 3.3 The WebObjects Application Wizard.

Examining the Project

After you have created the project, you will be presented with the Project Builder main window, as shown in Figure 3.4. The upper portion of the window allows you to browse your project; the lower portion is a text editor for files that you select.

Figure 3.4 The Project Builder main window.

The files in your project are divided into suitcases, each holding a different type of file. These suitcases are not necessarily subdirectories within your project directory, rather a logical way to group files. The number and type of suitcases depends on the type of project selected (see Table 3.1).

Table 3.1 Project Builder Suitcases and Their Contents for a WebObjects Application Project

Suitcase

Contents

Classes

Java and Objective-C class files. The initial contents should be Application.java, Session.java, DirectAction.java, and Main.java.

Headers

Header files for C, C++, or Objective-C files, not used in Java-only projects.

Other Sources

Anything else that needs to be compiled. This is where you would put C files. The file containing the main() function is located here.

Web Components

The components that make up your application are here. Each component is a folder that contains an HTML and WOD file. The class file for the component is in the Classes suitcase.

Resources

Any server-side resources that are not directly available to the client. The EOModel goes here.

Subprojects

You have the ability to break your project into a number of smaller projects, and these would go here. This is useful when working with a team of engineers.

Web Server Resources

Anything that is intended to be available to the client such as images.

Supporting files

Files that are used to create the application, but will not be deployed with the application. Makefiles are here, but it's rare that you would have to modify them. Templates for newly created files are also in this suitcase. These can be modified to add copyright and trademark information.

Frameworks

Frameworks consist of reusable code and resources, and these go here. If you purchase a third-party product framework, it would go here.

Libraries

Compiled libraries to be used with C, C++, or Objective-C code.

Non Project Files

This suitcase contains any files opened that are not part of your project.


Setting Preferences

One thing that you want to do in Project Builder is set the preferences appropriately (see Figure 3.5). The preferences window is displayed by selecting the Edit, Preferences menu items. Set the tab size in the Editing preferences. The Editing preferences are modified by clicking the Editing button in the left portion of the window. For some reason, the default is 8. Most developers prefer 4.

Figure 3.5 One task when using Project Builder is to set the tab size appropriately.

The editor in Project Builder helps the developer with editing the files through things like smart indentations. Depending on your coding style, the default values could either be really helpful or really detrimental. Most developers will find the default values quite irritating. If you fall into that category, do not throw your copy of WebObjects out the window, there are a number of options for customizing the way the Project Builder editor behaves. These options can be found under the Indentation part of the preferences dialog box (see Figure 3.6).

Figure 3.6 The Indentation part of the Preferences dialog box can be used to change the way the Project Builder editor behaves.

Project Settings

You can also set project options using the Inspector. The Build attributes allow you to customize how the application is built, including search orders and destination directories. Project Attributes allow you to change the name of your project (Figure 3.7). You have the option of changing the name of the directory in which the project is located when changing the project name.

Figure 3.7 The name of the project can be changed in the Project Inspector.

Project Find

Project Builder comes with some complex find functionality. You have the ability to search and replace text throughout the entire project. The Project Find is also smart enough to determine the differences between references to a term and the definition of the term (see Figure 3.8).

What results are displayed is dependent on the type of find. The different types and their effect are described in Table 3.2.

Figure 3.8 The Project Find panel. Clicking on the results displays the location where the item was found.

Table 3.2 Project Builder Find Types

Find Type

What's Displayed

Definitions

Where the text is defined in your code. This is useful when searching for a method definition.

References

This searches your code for where the item is defined and where references are made to the item. This returns a superset of the Definitions search. This is useful if you want to rename a method. You can rename the definition and everywhere it is referenced.

Textually

This is the classic find; the text is searched everywhere. If the text is found anywhere in the code in your project, it's displayed in the results.

Reg Expr'n

Stands for Regular Expression. This will search everything in your project. It is similar to the Textually find, but it extends to non-code files such as your HTML, WOD, and make files.


Class Browser

Another Project Builder feature is the class browser (see Figure 3.9). This allows you to browse the class hierarchy, viewing the methods for each class. One of the disadvantages of using the class browser for Java developers is that the documentation displayed is for Objective-C. Java developers should use the WOInfoCenter, or learn Objective-C.

Figure 3.9 The Class Browser dialog box.

Using WebObjects Builder

WebObjects Builder is used to edit your WebObjects components, where each component represents a page of your application. The components are located in the Web Components suitcase. You should have only one at this point, Main.wo. Double-click this component to open it in WebObjects Builder (see Figure 3.10).

Figure 3.10 WebObjects Builder is used to edit your WOComponents.

WebObjects Builder consists of an HTML editor, a component bindings editor, and a code generator. The top part of the WebObjects Builder window is used to edit the HTML for a component. The bottom part of the window displays the attributes for the component and allows you to browse key-paths for the component's attributes. Component bindings and code generation will be discussed later in this chapter. The HTML editor will be tackled first.

Editing HTML

There are two ways to edit HTML in WebObjects Builder, using the graphical and source editing modes. Graphical editing mode is a WYSIWYG editor that renders the HTML as you edit it (see Figure 3.11). Graphical editing mode gives you a good idea on how the final product will look, but it's no substitute for viewing the HTML in your browser. Using the graphical editing mode, you can change font sizes and styles, add HTML tags such as paragraphs, line breaks, and horizontal rules. You can also add and modify HTML tables.

Figure 3.11 The WebObjects Builder Graphical Editing Mode.

Source editing mode allows you to modify the HTML source. You can switch between source and graphical editing modes using the pop-up button on the toolbar all the way to the left. The icons for the different editing modes are described in Table 3.3.

Table 3.3 WebObjects Builder Editing Modes

Icon

Mode

Preview mode. WebObjects Builder will render your HTML.

Graphical editing mode. Somewhat of a WYSIWYG editing mode.

Source editing mode.


In the source editing mode, you can manually edit the HTML (see Figure 3.12). This is useful if you want to add tags that aren't available through the user interface or want to have more control over the editing. More than likely, the raw HTML that you're looking at is fairly ugly. To reformat it to look a bit better, select the Format, Reformat HTML menu item.

Figure 3.12 WebObjects Builder in source editing mode.

The bottom half of the screen allows you to modify the WebObjects definition file, which will be discussed later in this chapter.

Using HTML Tables

Any self-respecting Web page makes considerable use of HTML tables. Tables allow you to format the HTML in nice, even rows and columns. Tables can also be nested inside of one another for greater control. To easily create a HTML table, you need to be in graphical editing mode (see Figure 3.13). Select the HTML table icon.

Figure 3.13 The New Table dialog box.

The HTML table dialog box allows you to specify the rows, columns, and other table attributes. The table padding is the distance, in pixels, between the table rows and columns. The table spacing is the distance between the table cell border and the contents of the table cell. After you have decided on the table that you want, simply click OK to create it.

Use the Inspector to edit the table's structure after it has been created. You can modify the table's tag, the table data tags, or the table row tags. To modify a tag, click a point within the table. Notice that WebObjects Builder visually represents the tag that contains your cursor (see Figure 3.14). All tags up to the <BODY> tag are also represented.

Figure 3.14 The nested tags in WebObjects Builder.

To display the inspector for these tags, you can either click the Inspector button or right-click the tag and select Inspect <tag> from the pop-up menu (see Figure 3.15). On Macs, the pop-up menu is activated by holding down the Control key when clicking the tag.

Figure 3.15 The Table Inspector dialog box.

The table inspector allows you to change the structure of the table by adding and removing rows and columns (see Figure 3.16). You can also set the table settings such as the background color and the width and height.

Figure 3.16 The Table Row Inspector dialog box.

Inspecting a table row allows you to change the settings for an entire row such as the background color and the horizontal and vertical alignment (see Figure 3.17).

Figure 3.17 The Table Data Inspector dialog box.

The most atomic part of the table is the table cell tag, <TD>. (TD actually stands for Table Data.) When inspecting the table data tag, you not only can set the background color the same as the others, but also the horizontal and vertical alignment.

TIP

For almost all table cells, the vertical alignment should be set to top. The default value, center, is unappealing to most.

Back to the Movies...

It would be a good idea at this point to create a cool first page for your movies application. Make sure to save your changes in WebObjects Builder (see Figure 3.18).

TIP

You can tell if a document in WebObjects Builder is saved based on the name displayed in the title bar. If the name has an asterisk in it, it isn't saved.

Figure 3.18 WebObjects Builder with a sample first page. The formatting is done with a number of tables.

Building and Running

Your project can now be built and run from Project Builder in the same way you built and ran the example in Chapter 1, "Introducing and Installing WebObjects." The HTML for the main component is loaded dynamically, so if you make a change to the HTML, you just need to reload the page in your browser. If you change any Java code, however, your application will need to be rebuilt.

  • + Share This
  • 🔖 Save To Your Account