Retrieving the 'SlingHttpServletRequest request' from a parent node. | Adobe Higher Education
Skip to main content
kennyhank
Level 3
September 5, 2017
Besvarat

Retrieving the 'SlingHttpServletRequest request' from a parent node.

  • September 5, 2017
  • 21 svar
  • 10219 visningar

Hi Community!

For some reasons I need to retrieve the 'SlingHttpServletRequest request' from a parent node and I'm not be able to do it, I have already the request from the actual node but I'm passing on HTL an wcmmode='disabled' fixed value, this is why I'm going to need the parent request, sample code here:

          Resource resource = request.getResource();       

          Resource parent = resource.getParent();

          SlingHttpServletRequest requestParent = parent.adaptTo(SlingHttpServletRequest.class);

This is how I'm trying on the constructor on my class, but always I retrieve null as requestParent value.

I'll appreciate any suggestions on this, thanks in advance.

AEM6.2

Det här ämnet har stängts för svar.
Bästa svar av VeenaVikraman

Hi Kenny

     I am afraid , I am still not clear about your use case. Could you please try to explain your use case or requirement around the component little more clearly

   To answer your this question, (even though i didn't understand what your are trying to do here ) , you can always get a SlingHttpServletRequest object in your model using the code snippet I have mentioned in the below answer Re: Sightly question: how to adapt a class with a path

@Model(adaptables = SlingHttpServletRequest.class)

    Once you have the request you can always adapt a resource and play around

BUT, What i see in your code is you already have a request object ,

Resource resource = request.getResource();   

     Could you tell me what exactly are you trying to implement by fetching another request object (which doesn't make sense since you already have one . ) , when you can do almost everything using the Resource API .

PS:- All my above answers are direct to your question. Still I am not sure about your requirement . And as smacdonald2008 said, I don't think this is a correct approach.

21 svar

smacdonald2008
Level 10
September 6, 2017

100% agree - lets see what other community experts say!

VeenaVikraman
Community Advisor
Community Advisor
September 6, 2017

So here you are trying not to use the dialog capabilities of image and text (since you have disabled both the components , I assume you have the image and text authored in your 3rd component itself ). You just need the HTML of the image and text components respectively to be reused .

I will not recommend your approach as you just need the HTML part of these components and not the component features ; since you have disabled both the components which you have included. In HTL we have a feature called templates , which will perfectly suit your such requirement

Some pointers.

https://docs.adobe.com/docs/en/htl/docs/block-statements.html#template & call

Guide for working with Templates and Call in Sightly AEM 6.1 | Adobe AEM Club

May be a pretty googling will help you with more help guides on this topic. Let me know if you need more info around this

Prince_Shivhare
Community Advisor
Community Advisor
September 6, 2017

Hi Kenny,

I am agree with Veena_07​ .. Please reuse the dialog rather than entire component.

Regards,
Prince

kennyhank
kennyhankSkribent
Level 3
September 6, 2017

I have a Text and Image components, and I create a new one wich is a mixed of Text&Image component, everyone has its own behaviour. On the other hand I have another component called assetsharing wich I include as part of any component with assets capable to share, for instance I include this component on the last tab of Image and Text&Image components's dialog.

I'll never used my assetsharing component as a separate component, I always use this component as part of other depending of the use of the authors, author can choose if they want this behaviour on theirs components or not.

VeenaVikraman
Community Advisor
Community Advisor
September 6, 2017

          - I believe , you are reusing the assetshare component dialog to your Image or text component ? IS that correct ?

          If you are reusing the assetshare dialog then better to use the template feature of HTL . You can keep the assetshare HTML as a template in its .html file and try to use the same on each of your components

Your thoughts smacdonald2008Prince Shivharekautuksahni​ @Jorg Ratna Kumar

kennyhank
kennyhankSkribent
Level 3
September 6, 2017

I have Text, Image and Text&Image components, I'm including assetshare on Image and Text&Image, not on Text, text component is just text, nothing to share there.

I always use templates on my HTL code when I have to call more than one .html file on the same component, here I'm including  components for that reason I'm using:

text&image.html

.........HTL Text&Image component code here

<sly data-sly-resource="${'image' @ resourceType='xxx/components/content/image', wcmmode='disabled'}"></sly>

........more HTL Text&Image component code here

--------->

image.html

.........HTL Image component code here

<sly data-sly-resource="${'assetsharing' @ resourceType='xxx/components/content/assetsharing'}"></sly>

........more HTL Image component code here

---------->

assetsharing.html

..........HTL AssetSharing component code here

//wcmmode is ALWAYS DISABLED

Don't know how template features of HTL can solve this.

VeenaVikraman
Community Advisor
Community Advisor
September 6, 2017

Question -

          --> What is the purpose of including an image component here if you are not going to author this? Where do you do the authoring ? How is this different from using a <template> ?

(I know I am rounding back to where we have started . I understand you might have some purpose for doing this way and I really wanna understand the same to make sure any solution which we provide is in the right direction . It should solve your problem rather than confuse you more.)

kennyhank
kennyhankSkribent
Level 3
September 6, 2017

The reason is because on this mixed component (Text&Image) I have 3 scenarios:

1- Only text wich behaviour is the same as Text component.

2- Only image wich behaviour is the same as Image component.

3- Having both text and image on the same component.

So basically is more easily for author having these both components in only editable one component, and very useful, and the most important is a client requirement

smacdonald2008
Level 10
September 6, 2017

As community has stated - it makes more sense to write a standalone HTL component that is independent of other components.

No need to read other nodes or parent nodes when developing a component like this. It complicates matters. 

See this recently added article from Prince Shivhare​ that shows how to develop a standalone text/image component. There is no need to read other nodes, parent nodes, etc. All logic and CSS is for that component only --

Creating a custom Image Text component for Experience Manager

Hope this helps...

smacdonald2008
Level 10
September 6, 2017

THis is a really good community discussion however - i hope other community members provide their view!