Liferay 7 Portlet Preferences

admin

May 4, 2022

Portlet preferences are the persisted values of the portlet related preference data. Normally administrator uses preferences to provide a customization view to all users.  End User uses the preferences for configuring portlets just the way they like to see/behave.

Portlet preferences are stored in “key-value” pair. There is noticeable change in the way to implement it in Liferay 6.x and Liferay 7.

Let’s understand the steps for implementation in Liferay 7.

Create Liferay Module Project: Below is a sample structure.

Create Liferay Module Project

Important change required in gradle file.

compile group: ‘com.liferay’, name: ‘com.liferay.portal.configuration.metatype’, version: ‘2.0.0’

compile group: “biz.aQute.bnd”, name: “biz.aQute.bndlib”, version: “3.1.0”

 
A Config Handler class : responsible for storing and retrieving values of preferences.

// Component definition. Specify the appropriate configurationPid in the @Component annotation
@Component(

configurationPid = “com.enprowess.countryconfig.configuration.CountryConfiguration”,

configurationPolicy = ConfigurationPolicy.OPTIONAL,

 

// Service Declaration & Class inheritence
service = ConfigurationAction.class 

public class CountryConfigHandler extends DefaultConfigurationAction {

 

@Override
public void include(PortletConfig portletConfig, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

httpServletRequest.setAttribute(
CountryConfiguration.class.getName(), countryConfiguration);
super.include(portletConfig, httpServletRequest, httpServletResponse);
}

@Override
public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

String countryName = ParamUtil.getString(actionRequest, “countryName”);
setPreference(actionRequest, “countryName”, countryName);
super.processAction(portletConfig, actionRequest, actionResponse);
}

 

Capture Preferences : Here is the jsp file code.

<c:set value=<%= portletPreferences.getValue(“countryName”, countryConfiguration.getCountryName()) %> var=“selectedCountry”/>

// Define action URL and assign it to form submission

<aui:form action=<%= configurationActionURL %> method=“post” name=“fm”>

 

// Render the preference field
<aui:fieldset>
<aui:select name=“countryName” label=<B>Select Country</B> value=${selectedCountry}>
<aui:option value=“India”>India</aui:option>
<aui:option value=“United States”>United States</aui:option>
<aui:option value=“United Kingdom”>United Kingdom</aui:option>
<aui:option value=“Germany”>Germany</aui:option>
<aui:option value=“Canada”>Canada</aui:option>
</aui:select>
</aui:fieldset>

 
Retrieve value in portlet : MyBusinessLogicPortlet.java 

// Access preferences
PortletPreferences preference = renderRequest.getPreferences();
// Get Preference value, can be utilized or can be passed on to rendering jsp
countryNameValue = preference.getValue(“countryName”, countryConfiguration.getCountryName());

 

OUTPUT:
Config Menu:
Config Menu:

Select Preference:
Select Preference

 
Output in Display:
Output

 
Post By,
Manish Luste

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments