Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Creating a servlet for a component that is not under /content?

Avatar

Level 8

Hi

My goal is to create a token whenever a page is rendered that contains a component called someTeaser. This someTeaser component renders in the template after the content and before the footer and is not editable. It is configured fixed in the template structure.

The problem appears when I want to create a servlet for someTeaser. This component is not in the Content Repository crxde.

 

@SlingServletResourceTypes(
resourceTypes = {Constants.ResourceTypes.SOME_TEASER},
selectors = {"token"},
extensions = Constants.Extensions.JSON
)

 The resource for this component is /conf/xx/settings/wcm/templates/someTemplate/structure/jcr:content/root/main/container/someTeaser. This resource cannot be resolved in publish. It is not under content but rather under conf.

 

Request URL: https://publish_server/conf/xxx/settings/wcm/templates/someTemplate/structure/jcr:content/root/main/...

Request Method: GET

Status Code: 404 Not Found

 

An alternative solution would be to use define resourceTypes = {Constants.ResourceTypes.PAGE}  in the servlet but then every page will check if it has the selector token.

Is there a better solution for this?

 

4 Replies

Avatar

Community Advisor

Hi @anasustic 

 

Can you please be more clear on your query? I am not able to understand "This component is not in http://localhost:4506/crx for any detailHelp pages."

 

Thanks,

Kiran Vedantam.

Avatar

Level 8

Hi @Kiran_Vedantam yes, of course. I tried to be more clear on the original description.

Avatar

Employee Advisor

Looks like you are using editable templates in your site.

That means, that at rendering time the content of the template is merged with the content of the actual page (unlike static templates where the template is copied into the location where the resulting page is to be created). That means in the case of editable templates you don't find necessarily find all components below the page resources anymore (when using the repository browser).

 

So you can request your resource via /content although it's not persisted there in the JCR repository (as node).

 

Avatar

Level 8

Thanks @Jörg_Hoh 

I was able to request the token for the container that is avaliable in the JCR repository.

 

String containerPath =  PathBuilder.with(currentPage.getPath())
.slash(AemConstants.JCR_CONTENT)
.slash("root/main/container")
.build();

 and resolve the container url nicely using

resourceResolver.map(path)