In order to have configurations at the portlet level in Liferay, we can implement portlet preference. In this example, we have implemented a color configuration for portlet in Liferay 7.2.
Prerequisites
- Java
- Liferay portal 7/7.x
Environment Requirement
- JDK
- Eclipse
- MySQL
1) Create a module project with below classes.
2) Create a Java interface.
// ColorConfiguration.java
@Meta.OCD(id = ColorPreferencePortletKeys.CONFIGURATION_ID)
public interface ColorConfiguration {
@Meta.AD(deflt = "white",
name = "color",
optionLabels = { "%White", "%Red", "%Yellow" },
optionValues = { "white", "red", "yellow" },
required = false
)
public String color();
}
Where,
@Meta.OCD is used to register this class as a configuration with a specific id. The ID must be the fully qualified configuration class name.
@Meta.AD specifies the default value of a configuration field as well as whether it’s required or not. Note that if you set a field as required and don’t specify a default value, the system administrator must specify a value in order for your application to work properly. Use the deflt property to specify a default value.
This portlet requires the following dependencies.
// Dependencies
compileOnly group: “biz.aQute.bnd”, name: “biz.aQute.bndlib”,version: “3.1.0”
compileOnly’com.liferay:com.liferay.portal.configuration.metatype.api’
3) Create an action class.
Configuration action class is required to access portlet preferences. This class must extend the DefaultConfigurationAction class. It is responsible for storing portlet configurations.
// ColorPreferencesAction.java
package com.ignek.portal.color.preferences.action;
@Component(
configurationPid = ColorPreferencePortletKeys.CONFIGURATION_ID,
configurationPolicy = ConfigurationPolicy.OPTIONAL,
immediate = true,
property = "javax.portlet.name=" + ColorPreferencePortletKeys.PORTLET_ID,
service = ConfigurationAction.class
)
public class ColorPreferencesAction extends DefaultConfigurationAction {
private volatile ColorConfiguration colorConfiguration;
@Override
public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
String color = ParamUtil.getString(actionRequest, "color");
setPreference(actionRequest, "color", color);
super.processAction(portletConfig, actionRequest, actionResponse);
}
@Activate
@Modified
protected void activate(Map
4) Implement your portlet class.
// ColorPreferencesPortlet.java
package com.ignek.portal.color.configuration.portlet
@Component(
…
)
public class ColorPreferencesPortlet extends MVCPortlet {
private volatile ColorConfiguration colorConfiguration;
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
renderRequest.setAttribute(ColorConfiguration.class.getName(), colorConfiguration);
super.render(renderRequest, renderResponse);
}
@Activate
@Modified
protected void activate(Map properties) {
colorConfiguration = ConfigurableUtil.createConfigurable(ColorConfiguration.class, properties);
}
}
5) Now Implement your init.jsp.
// init.jsp
<%@page import="com.ignek.portal.config.ColorConfiguration"%>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%
ColorConfiguration colorConfiguration = (ColorConfiguration) renderRequest
.getAttribute(ColorConfiguration.class.getName());
String color = StringPool.BLANK;
if (Validator.isNotNull(colorConfiguration)) {
color = portletPreferences.getValue("color", colorConfiguration.color());
}
%>
6) Create configuration.jsp.
Add all configurations’s input fields in this JSP file.
// configuration.jsp
<%@ include file="/init.jsp" %>
White
Red
Yellow
7) Add the below code in your view.jsp.
// view.jsp
<%@ include file="/init.jsp"%>
Favorite color: <%=color%>
- Now, deploy this module.
- Add your portlet to a Liferay page.
- Click on the ellipsis icon and select the option Configuration.
- Change the configuration and save it.