Configuration interface

Accessing configuration is always done via the Configuration interface, which is basically a simple extension of a Sling ValueMap. Objects of this interface are obtained using adaptTo on a context object (Resource or SlingHttpServletRequest). The configuration context is detected automatically and an “effective” configuration with all parameters (configured values, inherited values, default values) is returned.

The API usage is really simple - example:

Configuration config = resource.adaptTo(Configuration.class);
String value = config.get("param1", String.class);

Or using a parameter constant provided by the defining application (recommended usage):

Configuration config = resource.adaptTo(Configuration.class);
String value = config.get(Params.PARAM_1);

In this case the type can be omitted as it is already part of the parameter definition. A default value can be supplied optionally.

Sling models

Example for accessing configuration from a Sling model class:

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class MyModel {

  @Self
  Configuration config;

  @PostConstruct
  private void activate() {
    String value = config.get("param1", String.class);
  }

}

Sightly template

Example for accessing configuration from a Sightly template:

<div data-sly-text="${config.param1}"></div>

The configuration data is provided inside the Sightly templates by a BindingsValuesProvider. Such a solution can be used for JSP or other scripting languages as well.

Back to top

Version: 0.5.7-SNAPSHOT. Last Published: 2019-03-01.