Home > Articles

WebObjects Components

📄 Contents

  1. Creating a New WebObjects Application
  2. WebObjects Components
  3. Using Images
  4. Summary
  5. Q&A
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.
Like this article? We recommend

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020