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
2. Create a Java interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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<Object, Object> properties) { colorConfiguration = Configurable.createConfigurable(ColorConfiguration.class, properties); } } |
4. Implement your portlet class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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<String, Object> properties) { colorConfiguration = ConfigurableUtil.createConfigurable(ColorConfiguration.class, properties); } } |
5. Now Implement your init.jsp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <%@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" %> <liferay-theme:defineObjects /> <portlet:defineObjects /> <% 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <%@ include file="/init.jsp" %> <liferay-portlet:actionURL portletConfiguration="<%=true%>" var="configurationActionURL" /> <liferay-portlet:renderURL portletConfiguration="<%=true%>" var="configurationRenderURL" /> <aui:form action="<%=configurationActionURL%>" method="post" name="fm"> <aui:input name="<%=Constants.CMD%>" type="hidden" value="<%=Constants.UPDATE%>" /> <aui:input name="redirect" type="hidden" value="<%=configurationRenderURL%>" /> <aui:fieldset> <aui:select label="Color" name="color" value="<%=color%>"> <aui:option value="white">White</aui:option> <aui:option value="red">Red</aui:option> <aui:option value="yellow">Yellow</aui:option> </aui:select> </aui:fieldset> <aui:button-row> <aui:button type="submit"></aui:button> </aui:button-row> </aui:form> |
7. Add the below code in your view.jsp.
1 2 3 4 5 6 7 | <%@ include file="/init.jsp"%> <p> Favorite color: <span style="color: <%=color%>;"><%=color%></span> </p> |