GateIn tutorial: how to create a Portlet

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

This tutorial will show you how to develop some basic portlets using GateIn portal along with some basic background for those that are new to Portlets.

A portlet is a pluggable user interface component that provides specific content, which could be a service or information from existing systems. Portlets provide the user interface of the portal by accessing distinct applications, systems, or data sources and generating markup fragments to present their content to portal users.

A typical example of a portlet can be a eather portlet that provides weather information for a city by accessing a Search Engine, or a facebook Portlet which shows recent messages that you have received.

So the primary responsibility of a portlet is to generate a markup fragment (such as HTML, XML, or WML), which is then displayed on a portal page within a window,called a portlet window. A portal page usually displays multiple portlets in distinct port-let windows, each with its own title and set ofbuttons to change its look and feel, set its preferences, and maximize, minimize, or remove the window.

Difference with Servlets: Unlike Servlets where the user interface isn’t componentized a portlet is responsible for generating the user interface specific to one visual component.

The responsibility of displaying the complete portal page remains of the portal server, which aggregates fragments generated by portlets and displays them in the portlet windows on the portal page. This division of responsibility makes it possible for a portlet to focus on generating its own user interface.

Libraries needed to run GateIn portlet application

The main dependency that you need when working with Maven and Portlets is:
{codecitation class="brush: xml; gutter: true;"}
      <dependency>
          <groupId>javax.portlet</groupId>
          <artifactId>portlet-api</artifactId>
          <version>2.0</version>
          <scope>provided</scope>
      </dependency>
{/codecitation}      
If you are compiling your GateIn Project using Eclipse, you need to provide the following import in order to compile your Portlets. (It is part of the GateIn distribution).
gatein tutorial gatein tutorial gatein tutorial gatein tutorial
So create a new Java Dynamic Web project and add a Java class named HelloWorldPortlet to it:
{codecitation class="brush: java; gutter: true;"}
package com.sample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderMode;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class HelloWorldPortlet extends GenericPortlet {

    @RenderMode(name = "view")
    public void showTime(RenderRequest request,
            RenderResponse response) throws
            PortletException, IOException {
            PrintWriter out = response.getWriter();
            out.println("Hello! Time is "+new java.util.Date().toString());
    }

}
{/codecitation}

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

The @RenderMode annotation for the showTime method informs that this method will use the portlet’s VIEW mode

The getWriter() method of RenderResponse is similar to the getWriter() method of HttpServletResponse, which returns a PrintWriterobject. 

The following table discusses about Portlet modes and the content generated in those modes

VIEW A portlet generates content that reflects the current state of the portlet instance.
This content carries business value. The GenericPortletclass provides the doView method, which is used to support the VIEW mode. It’s mandatory for portlets to support VIEW mode.
EDIT A portlet generates content that allows users to personalize the content and behavior of the portlet. This content doesn’t carry any business value; it's meant for users to specify their preferences. The GenericPortletclass provides the doEdit method, which is used to support the EDIT mode.
HELP A portlet generates content that shows help information. This content doesn’t carry any business value; it's meant to provide users with information on how to use the portlet. The GenericPortletclass provides the doHelp method, which is used to support the HELP mode.

Finally, before deploying your Portlet you need to provide a configuration file named portlet.xml:

{codecitation class="brush: xml; gutter: true;"}

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    version="2.0">
    <portlet>
        <portlet-name>HelloWorld</portlet-name>
        <portlet-class>com.sample.HelloWorldPortlet</portlet-class>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
        <portlet-info>
            <title>Hello World portlet</title>
        </portlet-info>
    </portlet>
</portlet-app>

{/codecitation}

This file needs to be placed into the WEB-INF folder of your Web application and contains the Portlets which are used by your applications (just like the web.xml does for your Servlets) and their rendering mode.

Follow us on Twitter