Documentation‎ > ‎Portlets‎ > ‎

Portlet Explained

Analysis

Portlets can be generated using tools like Rational Software Architect, NetBeans, Eclipse. Portlets follow same WAR package structure as those of servlets. It is essential to understand generated  portlet configuration file,  portlet java code and JSPs. This article explains all the generated files.

1. porlet.xml
 
In addition to web.xml, portlet needs portlet configuration file portlet.xml. The following is the listing of portlet.xml

<?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>
        <description>MyHelloWorld</description>
        <portlet-name>MyHelloWorld</portlet-name>
        <display-name>MyHelloWorld</display-name>
        <portlet-class>com.softwareorbit.myhelloworld.MyHelloWorld</portlet-class>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
            <portlet-mode>EDIT</portlet-mode>
            <portlet-mode>HELP</portlet-mode>
        </supports>
        <resource-bundle>com.softwareorbit.myhelloworld.messages</resource-bundle>
        <portlet-info>
            <title>MyHelloWorld</title>
            <short-title>MyHelloWorld</short-title>
        </portlet-info>
    </portlet>
</portlet-app>

Here are the key elements defined in portlet.xml
  • The title is defined in portlet-info element
  • The name of class with package prefix needs to be specified in porlet-class element
  • The supported modes needs to be specified in supports tag. This sample portlet is supporting VIEW, EDIT  and HELP modes

2. MyHelloWorld.java

The following is the portlet java class file that is defined in portlet-class element in portlet.xml.

public class MyHelloWorld extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {

    }
   
    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");       
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/MyHelloWorld_view.jsp");
        dispatcher.include(request, response);
    }
    public void doEdit(RenderRequest request,RenderResponse response) throws PortletException,IOException {
            response.setContentType("text/html");       
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/MyHelloWorld_edit.jsp");
        dispatcher.include(request, response);
    }
    public void doHelp(RenderRequest request, RenderResponse response) throws PortletException,IOException {

        response.setContentType("text/html");       
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/MyHelloWorld_help.jsp");
        dispatcher.include(request, response);
    }
}


The key points to understand
  • This portlet class extends GenericPortlet. The supplied JSR 286 GenericPortlet implements Portlet interface and provides default behavior.
  • Since portlet.xml defined to  support view, edit, help modes, a method corresponding to each mode needs to be overridden.
  • In this sample, the doView, doEdit, and doHelp methods delegate rendering contents to corresponding JSP. However, portlet can get PrintWriter using response object and write to the stream directly.
  • The processAction method does not do anything in this sample code. If there is a form submission, this method can get the submitted parameters from an action request object.
3. jsp/MyHelloWorld_view.jsp

The following is the contents of JSP that is included in the doView method to display MyHelloWorld

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects />

<b>
    MyHelloWorld
</b>


The definedObjects tag should be included in JSP. The invocation of this tag creates the following objects which can be used in JSP
  • renderRequest (actionRequest, resourceRequest depending on phase)
  • renderResponse (actionResponse, resourceResponse depending on phase)
  • portletSession
  • portletPreferences
4. MyHelloWorld_edit.jsp & MyHelloWorld_help.jsp

These JSPs also contain contents similar to above view JSP.

Comments