Setting and Accessing Configurations

Setting and Accessing Configurations

You can use Liferay’s configuration framework to add a settings UI for a MVC Portlet.

See the Example Portlet

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:

Sign in to Liferay at http://localhost:8080. Use the email address and the password test. When prompted, change the password to learn.

Then, follow these steps:

  1. Download and unzip Setting and Accessing Configurations.

    curl -O
  2. From the module root, build and deploy.

    ./gradlew deploy$(docker ps -lq)

    This command is the same as copying the deployed jars to /opt/liferay/osgi/modules on the Docker container.

  3. Confirm the deployment in the Liferay Docker container console.

    STARTED com.acme.n2f3.web.0.0 [1650]
  4. Verify that the example module is working. Open your browser to https://localhost:8080

  5. Add the N2F3 Portlet to a page. You can find the example portlet under Sample Widgets.

    Add the N2F3 Portlet to a page.

    The UI shows a welcome message along with the three configurable attributes: font color, font family, and font size.

  6. To change the configuration, navigate to Control PanelConfigurationSystem Settings. Under Other click category.n2f3.

    Click category.n2f3 under the Other category.

    Try entering a different font color, font family, and font size. Click the Update button and go back to your page with the published widget. Verify that the attributes have changed.

Here’s how the configuration framework works.

Creating the Configuration Interface

Defining configurable attributes in a configuration interface is enough to generate a configuration UI in System Settings.

In the sample project, the file is the configuration interface.

import com.liferay.portal.configuration.metatype.annotations.ExtendedObjectClassDefinition;

	category = "n2f3", scope = ExtendedObjectClassDefinition.Scope.COMPANY
	id = "com.acme.n2f3.web.internal.configuration.N2F3WebConfiguration",
	localization = "content/Language", name = "n2f3-configuration-name"
public interface N2F3WebConfiguration {

	@Meta.AD(deflt = "blue", required = false)
	public String fontColor();

Note, for this example interface the scope is set to Scope.COMPANY. See Scoping Configurations for more information.

The interface has three configurable attributes: font color, font family, and font size. Note that color and family are type string and size is type int.

Meta.OCD registers this class as a configuration with a specific ID.


Note that the ID must be the fully qualified class name (FQCN) of the configuration interface.

Meta.AD specifies optional metadata about the attribute such as a default value or whether the attribute is a required field. Note that if an attribute value is required but a default is not set, an administrator must set a value in settings for the application to work properly.

Next, see how the configuration is read by the MVC Portlet.

Reading the Configuration from the Application

  1. In the @Component annotation, the configuration interface class is specified with the configurationPid:

    configurationPid = "com.acme.n2f3.web.internal.configuration.N2F3WebConfiguration"
  2. To access the configuration, the render() method utilizes a ConfigurationProvider. The Configuration Provider API provides methods to retrieve a configuration at different levels of scope. The sample project’s configuration is instance scoped and uses the getCompanyConfiguration() method to retrieve the configuration.

	catch (ConfigurationException configurationException) {
		throw new PortletException(configurationException);

	super.render(renderRequest, renderResponse);

The configuration object is added to the request object and can now be read from the request of the application's JSP.

Accessing the Configuration from a JSP

  1. The following import statement adds the configuration interface to the JSP:

    <%@ page import="com.acme.n2f3.web.internal.configuration.N2F3WebConfiguration" %>
  2. The configuration object from the request object is obtained and the configuration values are read.

    N2F3WebConfiguration n2f3WebConfiguration = (N2F3WebConfiguration)request.getAttribute(N2F3WebConfiguration.class.getName());
  3. The attributes fontColor(), fontFamily(), fontSize() can now be used in the JSP.

Implementing a Dropdown Selection UI

The sample project has three attributes that can be configured. Currently the attributes must be manually entered into a text input field, but this can be further customized.

For example, you can use a dropdown list for the font family attribute instead of an input field. In the project’s configuration interface, replace the @Meta.AD annotation with this:

	optionLabels = {"Arial", "Georgia", "Helvetica", "Tahoma", "Verdana"},
	optionValues = {"arial", "georgia", "helvetica", "tahoma", "verdana"},
required = false)

Redeploy the example module.

The font family is now a dropdown selection.

Now the font family attribute is a dropdown selection.

ConfigurationBeanDeclaration with Previous Versions of Liferay


For Liferay DXP 7.4 U51+ and Liferay Portal 7.4 GA51+, a ConfigurationBeanDeclaration class is not required. The configration interface is registered with the Configuration Provider API automatically.

On Liferay versions before 7.4 Update/GA 51, the configuration class must be registered with a ConfigurationBeanDeclaration to use it with the Configuration Provider API. The ConfigurationBeanDeclaration class has one method that returns the configuration interface class. This helps the system keep track of configuration changes as they happen. For example, for the N2F3 portlet, create a class like this:

@Component(service = ConfigurationBeanDeclaration.class)
public class N2F3WebConfigurationBeanDeclaration
	implements ConfigurationBeanDeclaration {

	public Class<?> getConfigurationBeanClass() {
		return N2F3WebConfiguration.class;


In this example, place the class in the com.acme.n2f3.web.internal.settings.definition package.

Further Customization