Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How do I get a selector from a Sling Resource

kramer2718
Level 2
Level 2

I have two Sling Models:

    @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

    public class VideoGridItem {

   

      @SlingObject

      private Resource resource;

   

      @SlingObject

      private SlingHttpServletRequest slingHttpServletRequest;

   

   

      @PostConstruct

      public void initVideoGridItem() {

        String[] selectors = slingHttpServletRequest.getRequestPathInfo().getSelectors();

        insideGrid = selectors == null || selectors.length == 0 ? false : Arrays.stream(selectors).anyMatch("grid"::equals);

        url = URLUtils.addHTMLIfPage(resource.getResourceResolver(), linkUrl);

      }

    }

and

    @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

    public class VideoListing {

   

      private List<String> videoResourcePaths;

   

      @PostConstruct

      final void init() {

   

      }

   

    }

I call the VideoGridItem component (technically the resource which references the model) from the video-listing component using HTL:

      <sly data-sly-list.videoResourcePath="${model.videoResourcePaths}">

        <sly data-sly-resource="${videoResourcePath @ wcmmode='disabled', addSelectors='grid'}" data-sly-unwrap="true"></sly>

      </sly>

Now, when I debug the code, inside initVideoGridItem, slingHttpServletRequest is null. Fair enough, this resource isn't being directly requested, but I still need to be able to access the selector "grid". Is there a way I can do this from the VideoGridItem.resource?

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

Same is working for me.

HTL code -

<sly data-sly-resource="${'/content/AEM63App/en/products/help/dialog-even/jcr:content/par/dailogevents' @ wcmmode='disabled', addSelectors='grid'}"></sly> 

Java code -

this.message += "Request Path: " + request.getRequestPathInfo().getResourcePath() + "\n";

  this.message += "Selectors  " + request.getRequestPathInfo().getSelectorString() + "\n";

  String[] selectors = request.getRequestPathInfo().getSelectors(); 

          boolean insideGrid = selectors == null || selectors.length == 0 ? false : Arrays.stream(selectors).anyMatch("grid"::equals); 

  this.message += "inside grid  " + insideGrid + "\n";

Output -

Screenshot 2018-10-26 at 1.35.23 PM.png

View solution in original post

1 Reply
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

Same is working for me.

HTL code -

<sly data-sly-resource="${'/content/AEM63App/en/products/help/dialog-even/jcr:content/par/dailogevents' @ wcmmode='disabled', addSelectors='grid'}"></sly> 

Java code -

this.message += "Request Path: " + request.getRequestPathInfo().getResourcePath() + "\n";

  this.message += "Selectors  " + request.getRequestPathInfo().getSelectorString() + "\n";

  String[] selectors = request.getRequestPathInfo().getSelectors(); 

          boolean insideGrid = selectors == null || selectors.length == 0 ? false : Arrays.stream(selectors).anyMatch("grid"::equals); 

  this.message += "inside grid  " + insideGrid + "\n";

Output -

Screenshot 2018-10-26 at 1.35.23 PM.png

View solution in original post