C H A P T E R 13 |
Tutorial--Section 3.1 Create a Command Component |
This chapter describes how to create a Command component that can be reused by many command fields (button and HREF components) within the same application. This is the alternative technique to implementing request handling code in the command field's handle request event inside its parent page/pagelet class (handleLoginRequest in LoginPage, for example).
Commands provide great power and flexibility when it comes to code reuse. Any arbitrary Java class can become a Command component simply by implementing the com.iplanet.jato.command.Command interface. In this tutorial, you will create a new Command class using the Sun ONE Application Framework Command wizard to create a login/logout command which will replace the request handler event for the Login button. This Command component can then be reused by command fields on other pages and pagelets if required.
Create a Command component using the Sun ONE Application Framework Command wizard.
1. Select the main module folder, then click the Add Command button on the Sun ONE Application Framework toolbar.
The Select Command Type panel displays.
2. Enter UserAccessCommand in the Name textbox.
The UserAccessCommand component is added to the application.
Double-click the UserAccessCommand to open the Java source code for this component in the editor window.
It is quite a simple class that extends BasicCommand. BasicCommand implements the Command interface which declares only one method:
public void execute(CommandEvent) throws CommmandException
This command currently does nothing. You will need to add some code to the execute method to do what you need it to do, which is, to perform a user login or logout based on the operation name that is passed in via the CommandEvent parameter. The operation names are completely up to the developer (you). The following steps and tasks instruct you on how to pass and evaluate your custom operation names.
This step requires nothing more than writing a little code. This might seem like a lot of code, but much of it is reimplementation of the handleLoginRequest event from the LoginPage. This replaces the need for that button event.
Add the following code to the execute method of the UserAccessCommand class.
Before you can test run this code, you need to configure a command field (Button or HREF) to use it.
Configure the login button to use the UserAccessCommand component via the Command Descriptor property of the button. This also works the same for HREFS.
1. Expand the LoginPage node, and expand the Visual Components node.
2. Select the login button under Visual Components.
3. Click the ellipsis button for the Request Handler property.
This displays the Command Descriptor editor.
4. Select User-Defined Command (Default) from the list under the Create new shared instance radio button choice.
5. Change the Name property to userAccessCommand
6. Select the Component Properties tab at the bottom of the editor.
7. Click the ellipsis button for the Command Class Name property.
This displays the Command Class Chooser dialog.
8. Expand the Current Application Components node, then jatoturial, and then main.
9. Select the UserAccessCommand command component.
11. Change the Operation Name from DEFAULT to login.
Recall in the code what you implemented for the execute method in the UserAccessCommand class. You have an if/else block that is expecting either login or logout as an operation name. These are case sensitive, so you need to be sure you set this correctly, or you will receive the CommandException (Unknown operation name) when you test run this command.
12. Click OK to finish setting the Command Descriptor property for the login button.
Now, when you run the Login page and click the Login button, the UserAccessCommand component handles the request instead of the code in the handleLoginRequest event in the LoginPage.
You can leave the code in the handleLoginRequest event as is, because it will be never be invoked, unless you reconfigure the login button to use the request handler event instead of the command component.
This is because the Sun ONE Application Framework first looks for a Command Descriptor for the command field. If the Command Descriptor is not implemented, it then attempts to invoke the handle<CommandField>Request event. If the event is not implemented, you receive a request handler not found exception.
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.