Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

How to write modal class for inherited page property

Avatar

Level 7

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

6 Replies

Avatar

Community Advisor

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/hierarchi...

Avatar

Level 7

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

Avatar

Community Advisor

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

Avatar

Level 7
@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  

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 2

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