Prerequisites

I have used Liferay 6.0 GA4 Community Edition for this tutorial, bundled with GlassFish as application server because I like GlassFish – but Liferay is shipped with a rich variety of bundled execution environments so you may use GlassFish with a Tomcat, JBoss or Resin server if you wish to.

Creating a project with Maven

First we need a new Maven project – fortunately there is a Maven archetype for Liferay portlets .. it creates a standard wep application directory structure, dependencies needed, the portlet descriptor and some additional xml descriptor files for Liferay. If you’re not going to use Liferay you might want to delete the additional xml descriptors (or simple use the archetype ﻿org.apache.maven.archetypes:maven-archetype-portlet)

Because we didn’t have much to do here we’re going to declare some Liferay-specific portlet information.. first the liferay-display.xml we’re putting our portlet in the category ‘Samples’ .. we’re going to see what this means in the following screenshots of the Liferay user interface

Please note that editing of the Liferay descriptors is optional and we won’t change anything there in the following examples!

Now build the portlet war file via IDE or

mvn package

Installing and Adding the Portlet

Of course now that we have created a portlet we want to deploy it into our Liferay instance and add it to a page ..

First login to your Liferay instance with an account with sufficient permissions to deploy software .. if you’re using the bundled test instance try username/password test@liferay.com/test

Portlet Deployment Step 1 - Login

Select Manage > Control Panel from the top menu

Portlet Deployment Step 2 - Control Panel

Now go to Server > Plugins Installation > Install more Portlets > Upload File and upload your created war archive

Portlet Deployment Step 3 - Plugins Installation

Portlet Deployment Step 4 - Install Portlet

Portlet Deployment Step 5 - Upload File

Now you should be able to see the following success message

Portlet Deployment Success

Now click Back To Liferay > Add > More. Search for hascode or simply click on Sample (that’s the category that we’ve assigned to the portlet in the liferay-display.xml. Now you’re able to add the portlet to the screen using drag&drop or simply clicking Add

Selecting the portlet from the control panel

Adding the Portlet to the page

Inter-Portlet Communication

Now that we have written some portlets we want them to communicate ..

First we’re creating a Portlet to send a text from user input com.hascode.tutorial.portlet.EventSenderPortlet

Transferring custom objects between portlets

Now we’re going to send a custom object as a message from one portlet to another.Be aware that there are some limitations for such an object: It must implement Serializable, must have a no-arg-constructor and be annotated with @XmlRootElement

Finally our portlet looks like this and displays the keywords from the portlet configuration

Reading Portlet Preferences

Using Annotations

Since Portlet Specification 2.0 it is possible to use annotations to create portlet applications:

@RenderMode: Marks a method for execution for a specified render mode. The mode is defined using the annotation’s name flag.. e.g. @RenderMode(name = “view”)

@ProcessAction: Marks a method to be executed in a named action request

Let’s create a running example .. the following portlet offers a link, when the link is clicked, an action is triggered and the current time is displayed: com.hascode.tutorial.portlet.SimpleAnnotatedPortlet

JSF Portlet Bridge and Java Server Faces 2

Finally we’re going to get Java Server Faces running as a Portlet. This isn’t as easy as it sound because the JSF lifecycle contains much more phases than the Portlet lifecycle.

That’s why the JSF Portlet Bridges were invented and specified by the Java Community Process in JSR-301 (Portlet Bridge 1.0) and JSR-329 (Portlet Bridge 2.0). Please note that JSF’s target version for both bridges is 1.2 – afaik there is no JSR for a Portlet Bridge for JSF 2.x, please correct me here if I am wrong.

There are several vendors filling this gap – the one I chose here was Portletfaces that supports bridging between Portlet and JSF 2 lifecycle. If you want to take a deeper look into Portletfaces features, please consults its detailed documentation.

With this information and Portletfaces on board we’re going to create a JSF Portlet that displays a sorted list of strings from a ManagedBean. In addition we’re going to map the Portlet’s action modes EDIT, VIEW and HELP to a corresponding JSF instance so that the user is able to change the way the strings are sorted using the Portlet’s configuration mode.

First we need to add some dependencies and repositories for Java Server Faces and Portletfaces to our pom.xml

Now we need some Facelets for the different modes EDIT, HELP and VIEW .. let’s begin view the template for the view mode by creating a file named viewMode.xhtml in src/main/webapp/xhtml. We don’t do much here – we’re fetching the usernames from the ManagedBeans, iterate over them and output them in a html list

Having created our facelets we’re going to define a classical JSF navigation by creating the following faces-config.xml in src/main/webapp/WEB-INF. It simply says that an outcome success from the editMode leads to the viewMode.

Out JSF fragments do look fine now so it’s time that we arrange a date with the Portlet API using the Portlet bridge. The following declaration added to your portlet.xml enables the EDIT,VIEW,HELP modes and maps them to the corresponding JSF actions and uses org.portletfaces.bridge.GenericFacesPortlet as portlet bridge and mapper between portlet requests and jsf lifecycle.

This entry was posted
on Tuesday, July 19th, 2011 at 8:14 pm and is filed under Enterprise, Java.
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.