Apple WebObjects 3.5 User Manual
Page 45

Action Methods
45
// Java example
public Component showPart() {
Error errorPage;
Inventory inventoryPage;
if (isValidPartNumber(partNumber)) {
errorPage = (Error)application().pageWithName("Error");
errorPage.setErrorMessage("Invalid part number " +
partnumber);
return errorPage;
}
inventoryPage = (Inventory)
application().pageWithName("Inventory");
inventoryPage.setPartNumber(partnumber);
return inventoryPage;
}
Action methods don’t have to return a new page. They can instead direct
the application to use the request page as the response page by returning
nil
(
null
in Java). For example, in the Visitors application, the
recordMe
action
method in the Main page records the name of the last visitor, clears the text
field, and redraws itself:
// WebScript Visitors Main.wos
- recordMe
{
if ([aName length]) {
[[self application] setLastVisitor:aName];
[self setAName:@""]; // clear the text field
}
}
// Java Visitors Main.java
public Component recordMe
{
if (aName.length != 0) {
((Application)application()).setLastVisitor(aName);
aName = ""; // clear the text field
}
return null;
}
Note:
Always return
nil
(
null
) in an action method instead of returning
self
(
this
).
Returning
nil
uses the request component as the response component.
Returning
self
uses the current component as the response component. At
first glance, these two return values seem to do the same thing. However, if
the action method is in a component that’s nested inside of the request
component, a return value of
self
will make the application try to use the
nested component, which represents only a portion of the page, as the
response component. This, most likely, is not what you want. Therefore, it
is safer to always return
nil
.