1

I am trying to get page properties using Sling Models in AEM 6.3 but always I am getting null.

resultsRootPath = getCurrentPage().getProperties().get("ResultsRootPath", String.class);

Path of property:

/components/content/results/cq:dialog/content/items/column/items/ResultsRootPath

Could you please let me know the correct ways to property value using Sling Models?

Melebius
  • 6,183
  • 4
  • 39
  • 52
Ram
  • 27
  • 1
  • 1
  • 3
  • Is this a multi-valued property? – Imran Saeed Jan 03 '18 at 16:06
  • No, it's a single valued property. – Ram Jan 03 '18 at 16:36
  • Your property path looks like a component dialogue path. Can you check in jcr if the property exists and if the page property dialogue contains this property? – Imran Saeed Jan 04 '18 at 08:32
  • Yes, the property ResultsRootPath exits in the below page and same component model trying to get ResultsRootPath property /content/brcd/uk/en/insto/search-results/jcr:content/root/responsivegrid/search_results – Ram Jan 06 '18 at 08:38

4 Answers4

1

You can also do this:

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

    @Inject
    private InheritanceValueMap pageProperties;

    @Inject
    private ValueMap properties;

    @PostConstruct
    public void activate() {
        String pageString = pageProperties.getInherited("myproperty", "default"); //InheritanceValueMap gives pageProperties Value Map and getInherited is used to fetch a particular property.
    }

Refer this for more info: http://blogs.adobe.com/experiencedelivers/experience-management/valuemap-and-his-friend/

1

an alternative to these other options but might be simpler. the models know what page they are based on the SlingHttpServletRequest in the @Model section

@Model(
        adaptables = {Resource.class, SlingHttpServletRequest.class, SlingHttpServletResponse.class},
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class ModelClassName {

    @Inject
    private Page currentPage;

The @Inject private page currentPage will automatically grab the page and allow you access to all the properties etc.

0

In your model class you can create a field like this:

@Model(adaptables = Page.class)
public class ResultsPageModel {

    @Inject
    @Via("contentResource")
    @Named("ResultsRootPath")
    private Resource resultsRootPath;
}

or

@Model(adaptables = SlingHttpServletRequest.class)
public class ResultsPageModel {

    @Inject
    @Via("resource")
    @Named("ResultsRootPath")
    private Resource resultsRootPath;
}
Tomasz Szymulewski
  • 2,003
  • 23
  • 23
0

To take advantage of injector specific annotation, the better way will be:

@Model(adaptables = SlingHttpServletRequest.class)
public class MyModel {
  @ValueMapValue
  private String ResultsRootPath;

  public String getResultsRootPath(){ return ResultsRootPath; }
}

To mention, please use camel case for properties asin resultsRootPath.

Saravana Prakash
  • 1,321
  • 13
  • 25