
How to create a simple portlet
Table of Contents [-]
Sometimes developers might want to deploy a standard JSR-168 portlet, not needing Liferay's custom API. Here are a few steps how to achieve this (based on Liferay 4.3 / JBoss 4.2):
1. Create an empty web app (by hand or using your favourite tool. I used Eclipse WTP for this.)
2. Add portlet.jar to your project (only for compile time) (you may find it, for example, in the server/default/lib/ext folder of your liferay installation).
3. Write your portlet Java class, for example:
package de.prosystemsit.portal.test;
import java.io.IOException; import java.io.PrintWriter;
import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse;
public class GutenTagPortlet extends GenericPortlet{
@Override protected void doView(RenderRequest req, RenderResponse resp) throws PortletException, IOException { resp.setContentType("text/html"); PrintWriter out=resp.getWriter(); out.println("Einen wunderschönen guten Tag."); }
}
Note the "resp.setContentType()"; it is important.
4. The web.xml file might look like this:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>PortletTest</display-name> </web-app>
(not too complicated, right?)
5. Create a file liferay-portlet.xml in WEB-INF folder:
<?xml version="1.0"?> <liferay-portlet-app> <portlet> <portlet-name>guten_tag_portlet</portlet-name> <instanceable>true</instanceable> </portlet> </liferay-portlet-app>
6. Create a file liferay-display.xml in WEB-INF folder:
<?xml version="1.0"?> <display> <category name="category.test"> <portlet id="guten_tag_portlet" /> </category> </display>
This file tells Liferay where to show the portlet in the "Add Content" menu.
7. Create a properties file somewhere in the classpath, for example WEB-INF/classes/content/Language-ext.properties:
javax.portlet.title.guten_tag_portlet=Gutentagportlet javax.portlet.title=Gutenabendportlet category.test=Testkategorie
(I thought the correct name for the property was "javax.portlet.title.portlet_name", but that doesn't work. Having a property "javax.portlet.title" works for me, but I still don't know what to do if I have more than one portlet in the war file)
Note.: A rudimentary way, is to use a different Language.properties file for each portlet and configure which file your portlet uses on the portlet.xml file as described on the example below using the resource-bundle tag.
8. Create a file portlet.xml in WEB-INF folder. The resource-bundle element must refer to the properties file just created.
<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"> <portlet> <portlet-name>guten_tag_portlet</portlet-name> <display-name>Guten-Tag-Portlet</display-name> <portlet-class> de.prosystemsit.portal.test.GutenTagPortlet </portlet-class> <supports> <mime-type>text/html</mime-type> </supports> <resource-bundle>content.Language-ext</resource-bundle> </portlet> </portlet-app>
9. Create a .war file.
10. In Liferay, log in as administrator (joebloggs/test). Go to Admin portlet (select "Welcome Joe Bloggs" -> "My Places" -> "My Community" -> "Private Pages"). Click on "Plugins". "Install More Portlets". "Upload File". Select the WAR file you just created. Click "Install". Wait a moment - portlet is deployed in the background. (Wait until JBoss has stopped logging.)
11. Click "Back" to leave Admin portlet's full screen display. Select "Add Content" from Joe Bloggs' pull down menu. With a little luck, you will see your own category ("Testkategorie" in my example), and if you open it, you'll maybe see your portlet (in my example, I would expect "Gutentagportlet", but in fact it is "Gutenabendportlet", which I don't understand). Click "Add" and look what happens.
Unfortunately, I don't know how to use hot deployment features of my IDE if I deploy my .war using Admin portlet. But it is also possible to create a war file and deploy it by hand into JBoss without using Admin portlet. In this case, you have to add some listeners and servlets to your web.xml. Admin portlet does this web.xml-modification for you, so you can simply have a look at the generated web.xml and copy the changes into your own web.xml.
There is still some magic difference I couldn't figure out yet. If I deploy war file by hand, this will only work if Liferay is already running. Starting up JBoss with an already deployed war file won't work - I have to redeploy it when everything has booted up.