Forward from one Template to Another | Community
Skip to main content
Level 4
October 16, 2015
Solved

Forward from one Template to Another

  • October 16, 2015
  • 7 replies
  • 1446 views

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 ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by patK-ze1xYs

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> 

7 replies

joerghoh
Adobe Employee
Adobe Employee
October 16, 2015

Hi,

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

kind regards,
Jörg

patK-ze1xYsAuthorAccepted solution
Level 4
October 16, 2015

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> 

smacdonald2008
Level 10
October 16, 2015

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? 

Level 4
October 16, 2015

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. 

Level 4
October 16, 2015

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

smacdonald2008
Level 10
October 16, 2015

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

Level 4
October 16, 2015

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 ?