Table of Contents Previous Section
An action method is a method that's associated with a user action. You associate methods with a user action using a dynamic element. For example, WOSubmitButton has an attribute named action to which you can assign a method. When the submit button in the corresponding HTML page is clicked, the action method is invoked in the subsequent cycle of the request-response loop. This declaration in the HelloWorld application associates the action method sayHello with a submit button:
SUBMIT_BUTTON: WOSubmitButton {action = sayHello};
Clicking the submit button sends a request to the HelloWorld application, initiating a cycle of the request-response loop in which sayHello is invoked.
Action methods take no arguments and return a page that will be packaged with an HTTP response. For example, the sayHello action method of the HelloWorld example is defined as follows:
As in sayHello, most action methods perform page navigation. It is 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 component variable partnumber) or an inventory summary otherwise:
Action methods don't have to return a new page. They can instead direct the application to regenerate the request page. When an action method returns nil, the application uses the request component as the response component.
In the Visitors example, the request page is also used as the response page. The WebScript recordMe action method records the name of the last visitor and clears the text field:
- sayHello
{
id nextPage = [WOApp pageWithName:@"Hello"];
[nextPage setNameString:nameString];
return nextPage;
}
- 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;
}
- recordMe
{
if ([aName length]) {
[[self application] setLastVisitor:aName];
[self setAName:@""]; // clear the text field
}
return nil;
}