Documentation Archive Developer
Search
[an error occurred while processing this directive] PATH  Documentation > WebObjects 4.5 > Getting Started With WebObjects


   

Adding a Second Component

In this section, you'll create a new component. Instead of Java, you'll implement its code using WebScript. This section demonstrates the quick turnaround between development cycles when using WebScript.

  1. In Project Builder's browser, click Web Components in the first column.

  2. Choose File  New in Project.

    Note that the Web Components suitcase is selected.

  3. Type GuestList as the name of the new component, then click OK

    The WebObjects Component Wizard appears.

  4. Choose None for Available Assistance and WebScript for Component Language.

  5. Click Finish.

  6. In the second column of the browser, double-click GuestList.wo to bring up the component window in WebObjects Builder.

  7. Create a heading for this page, as you did for the Main component. Call it "Guest List" (or something else of your choosing), then press Enter twice.

  8. Add a WOString below the heading. After the WOString, type the text " guests have signed this guestbook." Press Shift-Enter twice.

    You're going to bind this WOString so that it reflects the number of guests who have submitted this form.

  9. In the object browser, click application.

    There is an entry in the second column for the allGuests application variable you created. This entry appears in the Main component as well, since application variables are accessible from anywhere in the code.

    If you click allGuests, you'll see in the third column an entry for count. This is a standard method that returns the number of objects in the array.

  10. Make a connection from count to the center rectangle to bind it to the WOString's value attribute.

  11. Save the GuestList component.

    You need to do one more thing so that the GuestList page now displays when the user submits the form.

  12. Go back to Project Builder and view the source code for Main.java. Replace the return statement in the submit method with the following code:

    return pageWithName("GuestList");

    pageWithName is a standard WebObjects method (defined in the WOApplication class) that allows you to specify a new page to display.

    At this point, the code for Main.java looks like this:

    // Generated by the WebObjects Wizard ...

    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 {
       protected Guest currentGuest;

       public Main() {
         super();
         currentGuest = new Guest();
       }

       public WOComponent submit() {
         ((Application)application()).addGuest(currentGuest);
         currentGuest = new Guest();
         return pageWithName("GuestList");
       }

    }

  13. Save Main.java.

  14. Build and run your application.

    Each time you submit the form, the number of guests displayed in the WOString should increase.

    To return to the Main page, you'll have to use your browser's backtrack button. Later in the tutorial, you'll add a hyperlink to return to the Main page.


© 1999 Apple Computer, Inc. – (Last Updated 24 Aug 99)