Apple WebObjects 3.5 User Manual
Page 44

Chapter 3
Common Methods
44
name and click the button. This initiates the application’s request-response
loop, and
sayHello
is invoked.
Action methods take no arguments and return a page (component) that will be
packaged with an HTTP response. For example, the
sayHello
method creates a
new page named Hello and sends that page the name the user has typed into
the text field.
//WebScript HelloWorld Main.wos
- sayHello
{
id nextPage;
nextPage = [WOApp pageWithName:@"Hello"];
[nextPage setVisitorName:visitorName];
return nextPage;
}
If you’re programming in Java, you can look at the HelloWorldJava example,
which is identical to HelloWorld but written in Java. Its
sayHello
method looks like
this:
//Java HelloWorld Main.java
public Component sayHello() {
Hello nextPage = (Hello)application().pageWithName("Hello");
nextPage.setVisitorName(visitorName);
return nextPage;
}
In this example, the component Main is used to generate the page that handles
the user request, and the component Hello generates the page that represents
the response. Main is the request component or the request page, and Hello is the
response component or the response page.
It’s common for action methods to determine the response page based on user
input. For example, the following action method returns an error page if the user
has entered an invalid part number (stored in the variable
partnumber
); otherwise,
it returns an inventory summary:
// WebScript example
- showPart {
id errorPage;
id inventoryPage;
if ([self isValidPartNumber:partnumber]) {
errorPage = [[self application] pageWithName:@"Error"];
[errorPage setErrorMessage:@"Invalid part number %@.",
partnumber];
return errorPage;
}
inventoryPage = [[self application] pageWithName:@"Inventory"];
[inventoryPage setPartNumber:partnumber];
return inventoryPage;
}