This guide provides a tutorial for creating a Groovy portlet with full-featured portlet modes.
Create the file HelloGroovy.groovy in a directory called groovy-simplest/WEB-INF/classes:
import javax.portlet.GenericPortlet; import javax.portlet.PortletContext; import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletPreferences; public class HelloGroovy extends GenericPortlet { public void doView(RenderRequest request, RenderResponse response) { PortletContext context = getPortletContext(); PortletRequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/view/hello-groovy-view.jsp"); rd.include(request, response); } public void doEdit(RenderRequest request, RenderResponse response) { PortletContext context = getPortletContext(); PortletRequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/view/hello-groovy-edit.jsp"); rd.include(request, response); } public void doHelp(RenderRequest request, RenderResponse response) { PortletContext context = getPortletContext(); PortletRequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/view/hello-groovy-help.html"); rd.include(request,response); } public void processAction(ActionRequest request, ActionResponse response) { String message = request.getParameter("message"); if (null != message && !"".equals(message)) { PortletPreferences prefs = request.getPreferences(); prefs.setValue("message", message); prefs.store(); } } }
You don't have to compile the source because it's groovy.
Create the file portlet.xml in the groovy-simplest/WEB-INF directory.
<?xml version="1.0" encoding="UTF-8"?> <portlet-app id="velocitysimplest" version="1.0"> <portlet id="HelloGroovy"> <portlet-name>HelloGroovy</portlet-name> <display-name>Hello Groovy Display Name</display-name> <portlet-class>org.apache.portals.bridges.groovy.GroovyPortlet</portlet-class> <init-param> <name>script-source</name> <!-- Note: You can set script source in three ways. The first is to use relative path uri, the second is to use file: url, and the last is to classpath: uri --> <!-- <value>/WEB-INF/groovy/HelloGroovy.groovy</value> <value>file:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/demo/WEB-INF/groovy/HelloGroovy.groovy</value> --> <value>classpath:HelloGroovy.groovy</value> </init-param> <!-- If auto-refresh is true, then a modification of script source applies instantly. --> <init-param> <name>auto-refresh</name> <value>true</value> </init-param> <supports> <mime-type>text/html</mime-type> <portlet-mode>VIEW</portlet-mode> <portlet-mode>EDIT</portlet-mode> <portlet-mode>HELP</portlet-mode> </supports> <supported-locale>en</supported-locale> <portlet-info> <title>Hello Groovy Title</title> <short-title>Hello Groovy Short Title</short-title> </portlet-info> </portlet> </portlet-app>
You don't have to add any special tags for this simple example, but you can add some tags for supporting Groovlet or Groovy template as a view page. Please see the groovy documentation for those.
<?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>Groovy Simplest</display-name> <description>The world's simplest Groovy portlet</description> </web-app>
Create the hello-groovy-view.jsp file in the groovy-simplest/WEB-INF/view directory. Put whatever content you desire in it. Here is an example:
<%@ page session="false" %> <%@ page import="javax.portlet.*"%> <%@ taglib uri='/WEB-INF/portlet.tld' prefix='portlet'%> <portlet:defineObjects/> <% String message = renderRequest.getPreferences().getValue("message", "Hello, Groovy!"); %> <h1><%=message%>!</h1>
<%@ page session="false" %> <%@ page import="javax.portlet.*"%> <%@ taglib uri='/WEB-INF/portlet.tld' prefix='portlet'%> <portlet:defineObjects/> <% String message = renderRequest.getPreferences().getValue("message", "Hello, Groovy!"); %> <form method="post" action="<portlet:actionURL/>"> Message: <input type="text" name="message" value="<%=message%>"> <input type="submit" value="Submit"> </form>
<H1>Hello Groovy Help</H1> <HR> <P>Groovy Portlet support rapid portlet application development.</P>
Copy the portals-bridges-groovy-1.0.jar, groovy-1.1.jar, antlr-2.7.6.jar, and asm-2.2.jar to the groovy-simplest/WEB-INF/lib directory. (The current version of groovy library was 1.1-beta-2 when the author wrote this guide. Please use the latest version of groovy.) IMPORTANT: Do NOT put the portlet-api-1.0.jar in the war file. If you have already built Jetspeed some of the 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/org.apache.portals.bridges/jars/portals-bridges-groovy-1.1.jar
From the directory groovy-simplest combine the files above into a war file using the command,
jar cvf ../groovy-simplest.war .
Copy the war file to $CATALINA_HOME/webapps/jetspeed/WEB-INF/deploy. Jetspeed-2 will deploy the webapp.
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.
def renderRequest = request.getAttribute("javax.portlet.request") def renderResponse = request.getAttribute("javax.portlet.response")