Configurable Portlet with Portlet Preferences

Portlet preferences can store data related to the configuration of a portlet. These stored preferences can change the view of the portlet dynamically. In this article, we will learn how to create a configurable JSP page for a Liferay portlet with portlet preferences.

Version details: Liferay version 6.2 + Apache Tomcat 7 server.

Adding Configurable portlet preferences

Create a Liferay MVC portlet and then add the <configuration-action-class> tag under the liferay-portlet.xml file as shown below. This tag is to identify the configuration action class for liferay.


Modify the portlet.xml file to enable config mode for portlet as shown below. The highlighted <portlet-mode> tag enables the config mode for the portlet. The highlighted <init-param>  is used for adding a customized configuration jsp page.


Add the following content to config.jsp file to provide the option to the users to configure the currency type.

<%@ taglib uri="" prefix="portlet" %>
<%@ taglib prefix="liferay-portlet" uri="" %>
<%@ taglib uri="" prefix="aui" %>

<portlet:defineObjects />
<liferay-portlet:actionURL var="ConfigurationURL" portletConfiguration="true"/>
<%String currency = portletPreferences.getValue("currency", "");%>

<aui:form action="<%=ConfigurationURL %>" method="post" >
 <aui:select name='currencyVal' lable="Add Currency">
 <aui:option value="Franc" selected='<%=currency.equals("Franc")%>'>Franc</aui:option>
 <aui:option value="Dollar" selected='<%=currency.equals("Dollar")%>'>Dollar</aui:option>
 <aui:option value="Euro" selected='<%=currency.equals("Euro") %>'>Euro</aui:option>
 <input type="submit" value="Save"/>

Now create an action class to handle the configuration changes. This class retrieves the currency value entered by the user to portlet preference.

package com.asb.config;
import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
import com.liferay.portal.kernel.util.ParamUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
public class ConfigClass extends DefaultConfigurationAction {
  public void processAction(PortletConfig portletConfig, ActionRequest actionRequest,  ActionResponse actionResponse) throws Exception { 
    super.processAction(portletConfig, actionRequest, actionResponse);
    PortletPreferences prefs = actionRequest.getPreferences();
    prefs.setValue("currency", ParamUtil.getString(actionRequest, "currencyVal"));;

Now add the following code to view.jsp file. This file will show the entered currency value.

<%@ taglib uri="" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@page import="java.lang.String" %>

<portlet:defineObjects />
String currency = portletPreferences.getValue("currency", "");
String currencyVal ="";
if(currency.equalsIgnoreCase("Dollar")) {
 currencyVal = "$";
else if(currency.equalsIgnoreCase("Euro")) {
 currencyVal = "€";
else {
 currencyVal = "₣";

<b style="font-size:30px;">Hey.!! Here is your </b>
<b style="font-size: 40px;">50 <%=currencyVal %></b>

Testing the output

Finally, the following are the view and configuration pages respectively.



In this article, we learned how to add configurable Liferay portlet preferences.