Configurable Portlet with Portlet Preferences

Portlet preferences are used to store data related to the configuration of a portlet.

These stored preferences can be used to change the view of the portlet.
Let us see how to create a configurable JSP page for a portlet with portlet preferences.

  • Create a Liferay MVC portlet.
  • Add the <configuration-action-class> tag under 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 is used to enable 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. This file provides option to the user 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="Dollor" selected='<%=currency.equals("Dollor") %>'>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 gets 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("Dollor")) {
 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>
  • Finally, following are the view and configuration pages respectively.

Check out all Liferay related posts.

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