Java tutorial on how to make a Portlet

Java tutorial on how to create a simple Hello world Portlet. Let’s start with a simple overview of files. Depending on your project structure and IDE your source code can be structured differently but in all normal cases you will have a WEB-INF folder and inside of that you should have a portlet.xml file. This is the file that defines what Portlets are included in our  application. You can have more than one portlet in one application.

portlet.xml


<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
	version="2.0">
	<portlet>
		<portlet-name>hello-world-tutorial</portlet-name>
		<display-name>Hello World Tutorial</display-name>
		<portlet-class>com.example.package.HelloWorldPortlet</portlet-class>
		<expiration-cache>0</expiration-cache>
		<supports>
			<mime-type>text/html</mime-type>
			<portlet-mode>view</portlet-mode>
			<window-state>normal</window-state>
		</supports>
		<portlet-info>
			<title>Hello World Tutorial</title>
			<short-title>Hello World Tutorial</short-title>
			<keywords>hello world, tutorial</keywords>
		</portlet-info>
		<security-role-ref>
			<role-name>administrator</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>power-user</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>user</role-name>
		</security-role-ref>
	</portlet>
</portlet-app>	

Now, we also need that class that we reference in portlet.xml. Every portlet needs a class that acts as the Controller in MVC pattern.

HelloWorldPortlet.java

import java.io.*;
import javax.portlet.*;

public class HelloWorldPortlet extends GenericPortlet {

	protected void doView (RenderRequest request, RenderResponse response) throws PortletException, IOException {
		String url = "/view.jsp";
		getPortletContext().getRequestDispatcher(url).include(request,response);  
	}
}

Finally, we also need the view.jsp file, this should go to the same level with WEB-INF folder.

view.jsp


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

<h2>Hello World</h2>
This is a tutorial portlet. <br />
Your browser seems to be :<b><%=request.getHeader("user-agent")%></b><br />
And we think that your current language is: <b><%=request.getLocale().getDisplayLanguage()%></b> <br />

Have a lovely day.

Please note that we used portlet_2_0 taglib which is JSR 286. If your environment requires an older version, just leave out the _2_0.

Now while this is a perfectly nice portlet, it’s a little bit too simple. For more complex applications I would suggest you use some framework to get a head start. Spring has a lovely Portlet support but you can also use Liferay provided MVCPortlet as your basis for new Portlets, but you can only do that when you develop your applications for Liferay Portal. Frameworks provide both functionality and structure to a project. Struts 2 enforces the MVC pattern and provides lots of functionality, such as custom tags and validating functionality. With Liferay you can use almost any frameworks in the JavaEE space, including Struts, Spring Framework, and JSF.

The benefit of doing a completely vendor-free(Not using any portal specific stuff) portlet with open-source frameworks is that you can deploy the same portlet in either Liferay, JBoss, Websphere Portal or other JSR 286 compliant portals.

Java tutorial

About Reigo Reinmets

Enterprise Software consultant with 12 years of experience in enterprise software world who's working on various projects that are mostly based on Liferay. Most blog posts here are real-life findings and issues we've encountered while working on projects and Java tutorials on Liferay.

Leave a Reply

Your email address will not be published. Required fields are marked *