Creating a Portlet in 6.1 CE

How do you develop a portlet for liferay? Since often asked and being ridiculously easy, I wrote down the steps here:

In this work-through, I assume you have successfully installed Liferay 6.1 CE as well as Eclipse Indigo and the Liferay SDK IDE.

Then you can start the standard eclipse "New... wizard" and chose to create a "Liferay project":

Give it a name and chose "Portlet" as the plugin type:

Of course, we will use the simpliest possible approach and thus choose Liferay MVC, the standard framework for creating portlets:

The IDE is now creating an empty Web-Project, complete with /docroot/WEB-INF and so forth.

Time to include the portlet. Start the "New Liferay portlet"-wizard available in the "New"-menu. Choose a meaningful name and let it subclass Liferay's "MVCPortlet". This will save you a lot of tedious work later on:

In the next dialog, you decide which modes (only view for the moment) your portlet will implement and whether JSPs and I18N-files are created. Click "Create resource bundle file":

The next dialog deals which CSS and JavaScript resources (leave untouched) and categorization. I chose to display hte portlet in the "Collaboration" section as well as in the Control Panel:

The last dialog page can be skipped. Finish the wizard. Your new project becomes populated with some default files now. Feel free to browse them:


 

 

 

 

 

  • Language.properties will hold the I18N messages.
  • Task.java is the Portlet itself - more on it below.
  • main.css - will hold your style classes - its empty now.
  • view.jsp of course is the user interface.
  • main.js may be included to use javascript code.
  • WEB-INF holds a lot of configuration code - lets take a look at it later.
  • build.xml is the ant build script.

 

 

 

 

 

 

 

 

Ok! Lets deploy this portlet. Use the context menu on the project to bring up the liferay build menu:

Hit "all" or "deploy" will fire up the Plugin SDK an deploy the project into your liferay. As usual, tail -f tomcat/logs/catalina.out is your friend for controlling deployment. If all runs well - and it should - you see the new portlet in the add menu of your page (don't be confused - I have deployed Social Office also and thus there is a Tasks portlet. But ours was called Task. Also, in a german installation like mine "collaboration" is called "Zusammenarbeit" ;-)

The output "This is the Task portlet in View mode" is generated from /docroot/html/task/view.jsp:

 
  

 

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

 

<portlet:defineObjects />

 

This is the <b>Task</b> portlet in View mode.

 
This JSP is in turn speified as an init-parameter to the portlet in /docroot/WEB-INF/portlet.xml. Liferays MVCPortlet the uses this init-parameter to render to view mode jsp.
 
So congrats! We just deployed a portlet successfully!

 

 

 

Blogs