Expand my Community achievements bar.

Multiple Selector in custom Sling Model

Avatar

Level 3

Hi Team,

 

Is it possible to register a custom sling model with 2 or more selectors ? If yes, can please share how this can be achieved. 

 

Can we use OSGI config to handle selector or resourceType to register a SlingModel ?

 

UseCase :

We have to export page content via custom Sling Model where in we can export complete page as JSON or specific component on that page as JSON as per selector used in the api endpoint.

 

Eg : <API ENDPOINT>.<SELECTOR1>.<COMPONENTID>.<EXTENSION>

 

Thank you in advance

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

7 Replies

Avatar

Community Advisor

Hi,

You can do it, you don't need to register the component with 2 sling exporter. you can register with one and pass component id.

example of model sling exporter with abcd id

 

http://localhost:4502/content/we-retail/language-masters/en.model.abcd.json 



Arun Patidar

Avatar

Level 3

 

Hi @arunpatidar 

Thank you!

 

We want to export an endpoint something like : 

http://localhost:4502/content/we-retail/language-masters/en.customselector.abcd.json 

Custom Selector means it can have sel1 or sel2


is it possible to maintain selectors in OSGI or resourceType ?

 

Thanks

Avatar

Community Advisor

Sling model can be registered with resourceType of your aem component.

I don’t think you need to register anything here. In your sling model you can read the sling selector from the url and send the response accordingly based on selector value. But in this case you need to adapt your sling model using sling servlet https://stackoverflow.com/questions/54457384/how-to-access-to-an-url-selector-at-a-component-level

 

Avatar

Level 3

Hi @DPrakashRaj 

 

Thank you!


Yes you are right but our use is different. I am trying to achieve something similar to this question https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/best-way-to-expose-existin...

 

All our component rendered using Sling Model.

 

This requirement is to expose existing/new pages as json via an endpoint similar to http://localhost:4502/content/we-retail/language-masters/en.customselector.abcd.json 

where "abcd" is the componentID which is unique for every component . When requested via endpoint should only return JSON of respective component.


Main point is whether multiple selector possible in sling Model. If yes, can it be customisable instead of fixed in the sling model.

 

Thank you for responding to my query

 

Avatar

Community Advisor

I believe you want same sling model to return different json output based on the selector in the url. If that’s the case then on your sling model you can read the selector and based on that value to  in can return the json of corresponding component.

final String selectorString = request.getRequestPathInfo().getSelectorString();

switch (selectorString) {
    case "fullreport":
        printFullReport();
        break;
    case "shortreport":
        printShortReport();
        break;
    case "printablereport":
        printPrintableReport();
        break;
}