How to write modal class for inherited page property | Community
Skip to main content
Level 6
Dec 28, 2021 6:54 AM UTC
Solved

How to write modal class for inherited page property

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.
Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
Dec 28, 2021 12:24 PM UTC
@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.

6 replies

Employee
Dec 28, 2021 4:44 PM UTC

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);

 

 

Translate
Kishore_Kumar_
Level 9
Dec 28, 2021 7:08 AM UTC

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

Translate
Ronnie09Author
Level 6
Dec 28, 2021 8:47 AM UTC

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

Translate
Kishore_Kumar_
Level 9
Dec 28, 2021 8:52 AM UTC

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

Translate