How to write modal class for inherited page property | Community
Skip to main content
Level 6
December 28, 2021
Solved

How to write modal class for inherited page property

  • December 28, 2021
  • 2 replies
  • 2108 views

I have created one page property i.e usp

In sightly usp data is comming by 

${inheritedPageProperties.usp}

 

but I want to write the modal class to do the same with same functionality. How should I do that?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kishore_Kumar_
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class UspModel {

    @HierarchicalPageProperty("usp")
    private String usp;

    public String getUsp() {
        return usp;
    }
}

 

@kishore_kumar_ and one more thing how to write JUnit for this  


Please try with SlingHttpServletRequest.class adaptable for below method.

 

@Inject
private InheritanceValueMap pageProperties;
String usp = pageProperties.getInherited("usp", "default");

and it will work without ACS common.

2 replies

Kishore_Kumar_
Level 9
December 28, 2021

You can use like below.

 

@Inject
private InheritanceValueMap pageProperties;
String usp = pageProperties.getInherited("usp", "default");

 

If you are using ACS Commons, we can use @HierarchicalPageProperty annotation.


https://adobe-consulting-services.github.io/acs-aem-commons/features/sling-model-injectors/hierarchical-page-property/index.html

Ronnie09Author
Level 6
December 28, 2021

It is working fine as per link using ACS Common.

 

I have one question I am using ACS COmmons therefore 

@Inject
private InheritanceValueMap pageProperties;
String usp = pageProperties.getInherited("usp", "default");

this would work?

I tried this it wasn't working but @HierarchicalPageProperty is working

Kishore_Kumar_
Level 9
December 28, 2021

What adapter are you using in your model? Pls copy paste your model class here 

Adobe Employee
December 28, 2021

You can also try this below snippet to access Inherited Page properties from Models

import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;

@Inject
Page currentPage;
...
InheritanceValueMap ivm = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
String inheritedValueMyCustomProperty = ivm.getInherited("myCustomerProperty", String.class);
String inheritedValueJcrTitle = ivm.getInherited("jcr:title", String.class);