This guide provides a tutorial for creating a very simple Velocity portlet with one template in the portlet view mode.
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
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>
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>
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
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.1.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.1.jar
From the directory velocity-simplest combine the files above into a war file using the command,
jar cvf ../velocitysimplest.war .