Expand my Community achievements bar.

SOLVED

Forward from one Template to Another

Avatar

Level 4

Following is the Template Code 

<% ...

 // based on the condition I am trying to forward to another "Page"

// I tried the following sling forward: 

<sling:forward path="/content/a/b" />

%>

I see the content of the page   "/content/a/b" (included in the forward) . But CSS / JS is all broken on the page. 

Is there a recommended way to forward the request to another Template ?

1 Accepted Solution

Avatar

Correct answer by
Level 4

ptK wrote...

Following is the Template Code 

<% ...

 // based on the condition I am trying to forward to another "Page"

// I tried the following sling forward: 

<sling:forward path="/content/a/b" />

%>

I see the content of the page   "/content/a/b" (included in the forward) . But CSS / JS is all broken on the page. 

Is there a recommended way to forward the request to another Template ?

 

 

I had to find a workaround for my code : 

SOLUTION : 

I had to intercept the request before it start executing the request and then use <sling:forward> 

View solution in original post

7 Replies

Avatar

Employee Advisor

Hi,

what do you mean with "CSS and JS are broken"?

kind regards,
Jörg

Avatar

Correct answer by
Level 4

ptK wrote...

Following is the Template Code 

<% ...

 // based on the condition I am trying to forward to another "Page"

// I tried the following sling forward: 

<sling:forward path="/content/a/b" />

%>

I see the content of the page   "/content/a/b" (included in the forward) . But CSS / JS is all broken on the page. 

Is there a recommended way to forward the request to another Template ?

 

 

I had to find a workaround for my code : 

SOLUTION : 

I had to intercept the request before it start executing the request and then use <sling:forward> 

Avatar

Level 10

Can you please explain in more detail why you want to forward a template. Do you mean you want to change the template on which a page is based during run-time? 

Avatar

Level 4

Jörg Hoh wrote...

Hi,

what do you mean with "CSS and JS are broken"?

kind regards,
Jörg

 

I dont see any JS / CSS that is declared at the header is getting loaded. 

Avatar

Level 4

smacdonald2008 wrote...

Can you please explain in more detail why you want to forward a template. Do you mean you want to change the template on which a page is based during run-time? 

 

On Template 1 (content.jsp)  I want to check if "PageA" exists on the content tree path ::  "/content/mysite/en_us/PageA"

I am using Resource  Resolver to check if Page exists , if exists I want to Forward the request to the Page Resource from JSP. 

I tried couple of options from https://sling.apache.org/documentation/bundles/sling-scripting-jsp-taglib.html about sling:forward .

 My Template Structure is : 

   Template 1

        |--- content.jsp  // Wrote a logic here to FWD the request to existing resource and not execute any script on the JSP.

Reason I am doing this is , I want the URL to be the same but want to include the content (Could not include sling:include as page will be loaded with double header and footer)  Both templates are different

Avatar

Level 4

smacdonald2008 wrote...

Have you tried using this API to forward requests to another page:

https://sling.apache.org/apidocs/sling5/org/apache/sling/api/scripting/SlingScriptHelper.html

 

Yes I tried the Request Dispatcher and it did not work. However I see illigal state exception on logs. (Seems like Response is committed) 

Still wondering if there is still a way to achieve this ?