Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

WCMBindings.CURRENT_PAGE is null in AEM6.1

Avatar

Employee

In AEM6.0, I had the following code to get the current page object. However, in AEM6.1, it's coming back null. 

public class LearnBindingProvider implements BindingsValuesProvider{
    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    public void addBindings(Bindings bindings) {
        //get sling object
        SlingScriptHelper slingResource = (SlingScriptHelper) bindings.get("sling");
        
        //get request object
        SlingHttpServletRequest request = (SlingHttpServletRequest) bindings.get("request");
        
        //get page object
        Page currentPage = bindings.containsKey(WCMBindings.CURRENT_PAGE) ?
                (Page)bindings.get(WCMBindings.CURRENT_PAGE) : null;

               

From the 6.1 documentation, I can't see that anything has changed in the WCMBindings object. 

http://docs.adobe.com/docs/en/aem/6-1/ref/javadoc/com/adobe/cq/sightly/WCMBindings.html

Any suggestions?

Thanks in advance. 

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi,

It is hard to tell without seeing the full class (including the annotations), but this is likely an ordering problem.

BindingsValuesProviders are evaluted in a specific sequence, based on the service ranking from lowest to highest. And there are actually two "loops". First, BindingsValuesProviders without a javax.script.name property are evaluated (starting with the lowest service.ranking and then moving up). And then, the BindingsValuesProviders with the javax.script.name property matching the script engine being used are evaluated (again, starting with the lowest ranking).

In this particular case, the currentPage binding is specified by the com.day.cq.wcm.core.impl.WCMBindingsValuesProvider component. In AEM 6.1, this has a service.ranking of 1000 and a javax.script.name property of 'jsp','sightly'.

As a result, for your BindingsValuesProvider to use bindings specified by this provider, you must specify a higher service.ranking and ensure that the javax.script.name is specified. For example, this code works:

@Component @Service @Properties({ @Property(name=Constants.SERVICE_RANKING, intValue=1001), @Property(name="javax.script.name", value={"jsp","sightly"}) }) public class BVPTest implements BindingsValuesProvider { @Override public void addBindings(Bindings bindings) { Page page = (Page) bindings.get("currentPage"); if (page == null) { bindings.put("pageTitle", "NULL"); } else { bindings.put("pageTitle", page.getTitle()); } } }

As for what changed between 6.0 and 6.1, I would have to look into that further. Perhaps the WCMBindingsValuesProvider didn't have a service.ranking. It appears that the ordering algorithm is the same in 6.0 and 6.1 (it was implemented in https://issues.apache.org/jira/browse/SLING-3320).

HTH,

Justin

View solution in original post

3 Replies

Avatar

Level 10

Seems like the  page is not available in the bindings, use sling binding resource as fall back.

Page currentPage;
                if (bindings.get(WCMBindings.CURRENT_PAGE) != null) {
                    currentPage = (Page) bindings.get(WCMBindings.CURRENT_PAGE);
                } else {
                    Resource resource = (Resource) bindings.get(SlingBindings.RESOURCE);
                    PageManager pageManager = resource.getResourceResolver().adaptTo(PageManager.class);
                    currentPage = pageManager.getContainingPage(resource);
                }

Avatar

Correct answer by
Employee

Hi,

It is hard to tell without seeing the full class (including the annotations), but this is likely an ordering problem.

BindingsValuesProviders are evaluted in a specific sequence, based on the service ranking from lowest to highest. And there are actually two "loops". First, BindingsValuesProviders without a javax.script.name property are evaluated (starting with the lowest service.ranking and then moving up). And then, the BindingsValuesProviders with the javax.script.name property matching the script engine being used are evaluated (again, starting with the lowest ranking).

In this particular case, the currentPage binding is specified by the com.day.cq.wcm.core.impl.WCMBindingsValuesProvider component. In AEM 6.1, this has a service.ranking of 1000 and a javax.script.name property of 'jsp','sightly'.

As a result, for your BindingsValuesProvider to use bindings specified by this provider, you must specify a higher service.ranking and ensure that the javax.script.name is specified. For example, this code works:

@Component @Service @Properties({ @Property(name=Constants.SERVICE_RANKING, intValue=1001), @Property(name="javax.script.name", value={"jsp","sightly"}) }) public class BVPTest implements BindingsValuesProvider { @Override public void addBindings(Bindings bindings) { Page page = (Page) bindings.get("currentPage"); if (page == null) { bindings.put("pageTitle", "NULL"); } else { bindings.put("pageTitle", page.getTitle()); } } }

As for what changed between 6.0 and 6.1, I would have to look into that further. Perhaps the WCMBindingsValuesProvider didn't have a service.ranking. It appears that the ordering algorithm is the same in 6.0 and 6.1 (it was implemented in https://issues.apache.org/jira/browse/SLING-3320).

HTH,

Justin

Avatar

Employee

Thanks very much. The ordering was the problem.