Monday, May 02, 2005

SAP Portals Continued

Now that we have downloaded and installed the Eclipse plug-in for SAP portal development, we can proceed with creating a project. Before we start, we need to decide which approach we plan on taking. SAP provides two different methods, PageContext and DynPage. We are going to use the DynPage approach because it is easier to integrate JSPs using the DynPage methodology.

To start, we go to the Eclipse menu and select File>New>Other... which will bring up a dialog box. The SAP plug-in has added a new choice, "Portal Application". Select this and two items will become available on the right side of the dialog box, "Create a portal application project" and "Create a new portal application object". Select "create a project," enter a name and directory for the project, and then push the "Finish" button. A new project is created which includes most of the jar files you will need. Two additional jar files need to be added because we are going to use the DynPage methodology, htmlb.jar and com.sap.portal.htmlbridge.jar. Add these to the project.

In order to write a DynPage, two classes are required. First, you need to create a launcher class. The launcher (or controller) must extend the SAP class, PageProcessorComponent and must provide an implementation for the abstract method getPage. The getPage method must return a DynPage object. Here is the beginning of our "Hello World" portlet:

package main;

import com.sapportals.portal.htmlb.page.*;
import com.sapportals.htmlb.page.*;
import com.sapportals.htmlb.*;
import com.sapportals.htmlb.enum.*;

public class TestController extends PageProcessorComponent {
 
 public DynPage getPage() {
  return new TestDynPage();
 }

The next step is to provide a DynPage. In this example, in order to keep it simple, we will not use a JSP but will generate HTML directly from our DynPage. The TestDynPage will be an inner class of the TestController. TestDynPage will extend DynPage and must provide an implementation for the three abstract methods, doInitialization, doProcessAfterInput, and doProcessBeforeOutput. We will provide empty implementations for the first two. The doInitialization method is invoked only the first time the user enters the page. The doProcessBeforeOutput is invoked after doInitialization the first time and after the doProcessAfterInput method on subsequent calls.

There are two other classes that we need to worry about. First, the Form class that provides us with the area of the screen that we can write on. The second class is the TextView class which is one of the SAP provided HTML-Business (htmlb) classes. These classes work in a very similar way to the JSF classes in that they automatically generate HTML and provide an event based methodology to respond to user actions. We will worry about events in our next attempt. In this example, we will simply send some text to the screen.

The steps are fairly simple. First we get the Form from the current DynPage. Next we create a TextView, add some text to it, and then add it to the Form. And that is it. All that is left is to look at the rest of the code:

 public static class TestDynPage extends DynPage {
  
  public void doInitialization() {}

  public void doProcessAfterInput() throws PageException {}

  public void doProcessBeforeOutput() throws PageException {
   Form aForm = this.getForm();
   TextView label = new TextView();
   label.setText("Hello World!");
   label.setDesign(TextViewDesign.LABEL);
   aForm.addComponent(label);
  }
 }
}

No comments: