Jetspeed Simple Velocity Portlet Guide

This guide provides a tutorial for creating a very simple Velocity portlet with one template in the portlet view mode.

The latest release of Portals Velocity Bridge is version 2.1

1. The Portlet Class

Create the file VelocitySimplest.java in a directory called velocity-simplest/WEB-INF/classes:

public class VelocitySimplest extends org.apache.portals.bridges.velocity.GenericVelocityPortlet
{

    public void doView(javax.portlet.RenderRequest request, javax.portlet.RenderResponse response)
                throws javax.portlet.PortletException, java.io.IOException
    {
        super.doView(request, response);
    }
}
				

Compile the class in the velocity-simplest/WEB-INF/classes directory using the command,

javac -cp portlet-api-1.0.jar:portals-bridges-velocity-1.0.jar:portals-bridges-common-1.0.jar VelocitySimplest.java
				

2. The portlet.xml

Create the file portlet.xml in the velocity-simplest/WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app id="velocitysimplest" version="1.0">
  <portlet id="VelocitySimplest">
    <portlet-name>VelocitySimplest</portlet-name>
    <display-name>Velocity Simplest Display Name</display-name>
    <portlet-class>VelocitySimplest</portlet-class>
    <init-param>
        <name>ViewPage</name>
        <value>/WEB-INF/view/world.vm</value>
    </init-param>
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>VIEW</portlet-mode>
    </supports>
    <supported-locale>en</supported-locale>
    <portlet-info>
      <title>Velocity Simplest Title</title>
      <short-title>Velocity Simplest Short Title</short-title>
    </portlet-info>
  </portlet>
</portlet-app>
			

3. The web.xml

Create the file web.xml in the velocity-simplest/WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>Velocity Simplest</display-name>
  <description>The world's simplest Velocity portlet</description>

  <!-- Define Velocity Servlet -->
  <servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.portals.bridges.velocity.BridgesVelocityViewServlet</servlet-class>
  </servlet>

  <!-- Map *.vm files to Velocity  -->
  <servlet-mapping>
    <servlet-name>velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
  </servlet-mapping>

</web-app>
			

4. The View

Create the world.vm file in the velocity-simplest/WEB-INF/view directory. Put whatever content you desire in it. Notice that the template file is defined in the portlet init parameter ViewPage. The objects PortletConfig, RenderRequest, and RenderResponse are automatically placed in the Velocity context for use in Velocity templates. Here is a sample template showing a few of these objects methods and properties.

$portletConfig.portletName
$portletConfig.portletContext.serverInfo
#set ($MESSAGES = $portletConfig.getResourceBundle($renderRequest.Locale))
$renderRequest.portletMode
$renderResponse.namespace

5. The Dependency JARs

Copy the commons-beanutils-1.7.0.jar, commons-collections-3.1.jar, commons-digester-1.7.jar, portals-bridges-velocity-1.0.jar, velocity-1.4.jar, and velocity-tools-1.2.jar to the velocity-simplest/WEB-INF/lib directory. IMPORTANT: Do NOT put the portlet-api-1.0.jar in the war file. If you have already built Jetspeed these jars should be in your Maven repository. If so executing these commands in the lib directory will set up the dependencies for you.

ln -s ~/.maven/repository/commons-beanutils/jars/commons-beanutils-1.7.0.jar
ln -s ~/.maven/repository/commons-collections/jars/commons-collections-3.1.jar
ln -s ~/.maven/repository/commons-digester/jars/commons-digester-1.7.jar
ln -s ~/.maven/repository/org.apache.portals.bridges/jars/portals-bridges-velocity-1.0.jar
ln -s ~/.maven/repository/velocity/jars/velocity-1.4.jar
ln -s ~/.maven/repository/velocity-tools/jars/velocity-tools-1.2.jar

6. The WAR file

From the directory velocity-simplest combine the files above into a war file using the command,

jar cvf ../velocitysimplest.war .
			

7. Deploy the WAR file

Copy the war file to $CATALINA_HOME/webapps/jetspeed/WEB-INF/deploy. Jetspeed-2 will deploy the webapp.

8. The PSML

Create the PSML page using the Jetspeed portlet chooser. Login and click on the edit page icon. Your user must have the permission to edit pages. The user admin password admin has permission to edit all pages.