Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Every WebObjects application includes a Main component by default. The HTML file is mostly empty except for "Hello World." Likewise, the Java file contains very little. We will learn how to add methods and variables to the component by building a very basic calculator.

Adding Methods

Let's start by editing the Main.java file.

  1. Click on the Main WO component and then double-click the related Main.java file.
  2. Add the following public variables to the Main class:
    Code Block
    
    public double firstUserInput;
    public double secondUserInput;
    public double result;
    
  3. Add the following public method to the Main class:
    Code Block
    
    public WOComponent addResult(double firstUserInput, double sendUserInput) {
    		result = firstUserInput + secondUserInput;
    		return null;
    		}
    
  4. Your resultant Main.java should look something like this:
    Code Block
    
    // Generated by the WOLips Templateengine Plug-in at Mar 19, 2008 11:00:05 AM
    package your.app.components;
    
    import com.webobjects.appserver.WOComponent;
    import com.webobjects.appserver.WOContext;
    
    public class Main extends WOComponent {
    	private static final long serialVersionUID = 1L;
    	public double firstUserInput;
    	public double secondUserInput;
    	public double result;
    
    	public Main(WOContext context) {
    		super(context);
    	}
    
    	public WOComponent addResult() {
    
    result = firstUserInput + secondUserInput;
    
    return null;
    
    }
    }
    
  5. Delete "Hello World" within the <body> tags.
  6. Click on the "Insert WOForm Component" in the menu bar.

Figure 2-1 The Eclipse Toolbar with WOLips installed

...