concept GenericPortlet in category java

appears as: GenericPortlet, GenericPortlet
Portlets in Action

This is an excerpt from Manning's book Portlets in Action.

A portlet class must implement the javax.portlet.Portlet interface, directly or indirectly. The portlet API has a GenericPortlet abstract class that implements the Portlet interface and provides a default implementation for its methods. Developers will usually subclass the GenericPortlet class and override one or more methods to provide a specific implementation. The HelloWorldPortlet class is an example of a portlet class.

The next section covers the Portlet interface and GenericPortlet class, which are used to create portlets.

The UserRegistrationPortlet class extends the GenericPortlet class and makes use of @RenderMode and @ProcessAction annotations to specify the portlet’s render and action methods. The following code snippet shows the init method:

private String defaultEmail;
public void init() {
  defaultEmail = getPortletConfig().getInitParameter("defaultEmail");
}

The init method makes use of the GenericPortlet class’s getInitParameter convenience method to obtain the defaultEmail portlet initialization parameter from the portlet deployment descriptor.

The GenericPortlet class provides a default implementation for all the methods defined in the different lifecycle interfaces (Portlet, ResourceServingPortlet, and EventPortlet), along with convenience methods, making it easy for developers to write their own portlets. There are several benefits of extending the GenericPortlet class to create your own portlet:

  • Your portlet class isn’t directly dependent upon the portlet lifecycle interfaces. If the portlet lifecycle interfaces undergo any changes in future versions of the Java Portlet specification, those changes can be absorbed by the GenericPortlet class, making your portlets forward compatible.
  • GenericPortlet provides convenience methods that make it easy for you to handle requests. For example, the default implementation of the render method in GenericPortlet attempts to dispatch a render request to a doView, doEdit, or doHelp method, depending upon the portlet mode. We’ll discuss portlet modes and the doView, doEdit, and doHelp methods later in this chapter.
  • GenericPortlet makes use of the Java annotation facility to dispatch requests to the appropriate method. This allows you to give developer-friendly names to your methods so you can recognize them easily. We’ll discuss the annotations available in the GenericPortlet class later in this chapter.
  • Figure 2.9 shows that the GenericPortlet class implements lifecycle interfaces and provides convenience methods. For brevity, not all methods of the GenericPortlet class are shown in the figure.

    Figure 2.9. The GenericPortlet abstract class implements the Portlet, EventPortlet, and ResourceServingPortlet interfaces. It provides default implementations of the Portlet interface’s render method, and it dispatches render requests to the doView, doHelp, and doEdit methods, depending upon the portlet’s mode.

    To gain the benefits offered by the GenericPortlet class, the User Registration example portlet extends the GenericPortlet class.