Sling Model with custom selector not working in AEM 6.5 | Community
Skip to main content
Level 2
November 18, 2021

Sling Model with custom selector not working in AEM 6.5

  • November 18, 2021
  • 2 replies
  • 7780 views

Hi All,

 

I am using a Sling model exporter to export json data in AEM 6.5 and using the selector "caas". When the selector is "model" , it works perfectly. However with custom selector , it shows the below error.

 

Invalid recursion selector value 'caas'

Cannot serve request to /content/abc/internal/exportsite/en/clientaccess/help/secondlevela/level3.caas.json in org.apache.sling.servlets.get.DefaultGetServlet

 

Already tried the below:

check if this helps - Re: Sling Exporter with custom selector

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
November 18, 2021

Hi @vikrams57194913 

 

Is /content/abc/internal/exportsite/en/clientaccess/help/secondlevela/level3 a page?

Did you try with /content/abc/internal/exportsite/en/clientaccess/help/secondlevela/level3/jcr:content.caas.json?

 

Thanks!

Level 2
November 18, 2021

I am getting blank page with the above link also, only getting response from model.json

 

Thanks

Level 2
November 19, 2021

We need to provide implementation for all methods and delegate. We can make use of lombok @Delegate annotation for this - https://www.initialyze.com/blog/2020/11/simplify-extending-sling-models-with-lombok/

 

Also, note that your custom interface(PageExporter) has methods in the same name as OOTB core Page interface. Cross check the same and rename/remove based on your need. 

 

You can use the below and see if it works. 

package com.aem.demoproject.core.models;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.day.cq.wcm.api.Page;
import lombok.experimental.Delegate;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

import javax.inject.Inject;
import java.util.Map;

@Model(adaptables = SlingHttpServletRequest.class, adapters = {com.adobe.cq.wcm.core.components.models.Page.class}, resourceType = PageExporterImpl.RESOURCE_TYPE)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, selector = "mobile", extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class PageExporterImpl implements com.adobe.cq.wcm.core.components.models.Page {

    protected static final String RESOURCE_TYPE = "demoproject/components/structure/page";

    @Inject
    private Page currentPage;

    @Delegate(types = com.adobe.cq.wcm.core.components.models.Page.class, excludes = Handled.class)
    @Self
    @Via(type = ResourceSuperType.class)
    private com.adobe.cq.wcm.core.components.models.Page pageDelegate;

    @Override
    public Map<String, ? extends ComponentExporter> getExportedItems() {
        return pageDelegate.getExportedItems();
    }

    public String getMessage() {
        return "Custom Page Exporter Impl";
    }

    protected static interface Handled {
        public Map<String, ? extends ComponentExporter> getExportedItems();
    }
}

Result :

 


Getting below error with the above code:
<h1>Error during include of component
'/apps/contentservices/components/structure/page'</h1><h3>Error Message:</h3> <pre>org.apache.sling.models.factory.MissingElementsException:
Could not inject all required fields into class
ca.demo.web.contentservices.core.impl.PageExporterImpl</pre><h3>Processing Info:</h3> <table style='font-family: monospace'> <tr><td>Page</td><td>=</td><td>
/content/demo/internal/exportsite/en/clientaccess/help/secondlevela/level3<td></tr><tr>
<td>Resource Path</td><td>=</td><td>/content/demo/internal/exportsite/en/clientaccess/help
/secondlevela/level3/jcr:content<td></tr><tr><td>Cell</td><td>=</td><td>page<td></tr><tr>
<td>Cell Search Path</td><td>=</td><td>page|basicpage<td></tr><tr><td>Component Path</td>
<td>=</td><td>/apps/contentservices/components/structure/page<td></tr></table> <h3>Sling Request Progress:</h3>

I also applied defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
in the model, after that getting Null pointer exception on language etc.

Thanks
September 21, 2022

Hi Vikram, 

Add the caas.json.jsp file in the project and update its content as below: 

 

/apps/cq/Page/caas.json.jsp 

<%@include file="/libs/cq/Page/proxy.jsp" %>

 

Update the workspace filter so changes are picked up: 

<filter root="/apps/cq/Page">

 

This should work. You can add multiple selectors with this way. 

 

Another solution is to combine your custom selector with the OOTB model selector e.g model.caas