Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

RequestAttributes- data-sly-include

Avatar

Level 5

Hello All - Can I use the request-attributes in data-sly-include like the below so that I can use the values in JSP.
E.g.
<sly data-sly-include="${ '/apps/project-name/components/../manipulate.jsp' @ requestAttributes='abc.html'}" />

 

Please confirm if the syntax is correct. if so,  what is the parameter name should I use it to get the value?

<%= request.getParameter("param") %>

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @v1101 

 

In order to pass values to JSP, you are in right direction to use requestAttributes. But the parameter requestAttributes takes a map instead of a single value. In order to set a map and then pass as request parameter refer https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html#reques....

 

 

public class Settings extends WCMUsePojo { 
// used to pass is requestAttributes to data-sly-resource 
  public Map<String, Object> settings = new HashMap<String, Object>(); 

@Override public void activate() throws Exception { 
    settings.put("property1", "abc.html"); 
  }
}

 

you can also use sling model to set key value pairs.

 

you can then call jsp like this:

 

<sly data-sly-use.myclass="com.adobe.examples.htl.core.hashmap.Settings" data-sly-include="${ '/apps/project-name/components/../manipulate.jsp' @ requestAttributes=myclass.settings}" />

 

 
In JSP file, you can get this property value like :

 

<%@include file="/libs/granite/ui/global.jsp"%>
<%=request.getAttribute("property1")%>​

 

 

you can not use getparameter as it will provide you query parameter in the URL.

 

Hope it helps!

Thanks!

Nupur

 
 

View solution in original post

9 Replies

Avatar

Employee

Hi @v1101 ,

Yes, you can pass request-attributes [0] to data-sly-include and data-sly-resource in order to use them in the receiving HTL-script. This allows you to properly pass-in parameters into scripts or components:

 

<sly data-sly-include="${ 'something.html' @ requestAttributes=a_map_of_attributes}" />


[0]: https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html#reques...

 

In regards to accessing the parameter name, check https://stackoverflow.com/questions/1890438/how-to-get-parameters-from-the-url-with-jsp for more details.

 

 

Thanks!!

 

 

 

Avatar

Correct answer by
Community Advisor

Hi @v1101 

 

In order to pass values to JSP, you are in right direction to use requestAttributes. But the parameter requestAttributes takes a map instead of a single value. In order to set a map and then pass as request parameter refer https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html#reques....

 

 

public class Settings extends WCMUsePojo { 
// used to pass is requestAttributes to data-sly-resource 
  public Map<String, Object> settings = new HashMap<String, Object>(); 

@Override public void activate() throws Exception { 
    settings.put("property1", "abc.html"); 
  }
}

 

you can also use sling model to set key value pairs.

 

you can then call jsp like this:

 

<sly data-sly-use.myclass="com.adobe.examples.htl.core.hashmap.Settings" data-sly-include="${ '/apps/project-name/components/../manipulate.jsp' @ requestAttributes=myclass.settings}" />

 

 
In JSP file, you can get this property value like :

 

<%@include file="/libs/granite/ui/global.jsp"%>
<%=request.getAttribute("property1")%>​

 

 

you can not use getparameter as it will provide you query parameter in the URL.

 

Hope it helps!

Thanks!

Nupur

 
 

Avatar

Level 5
Thanks nupurjain for the response. Basically I want to pass the specific clientlib category details of the component to the JSP and the category details may vary based on every component.

Avatar

Community Advisor

Hi @v1101 

 

In the similar way, you can send one key value pair with value equals to clientlib category to the JSP include.

Avatar

Level 5
If I have multiple values configs in pojo class. e.g settings.put("property1", "abc.html"); settings.put("property2", "sample1.html"); settings.put("property3", "sample2.html"); How do I pass specific item say 'property2' in the data-sly-include?

Avatar

Community Advisor

@v1101 

Even if there are multiple key value pairs, there is no harm sending all pairs in include. We can not add specific pair from Map to requestAttribute, we have to send the whole map. OR either create new map with only required property and then send in requestAttribute. 

Avatar

Level 5

Hi @nupujain - I am getting an error while trying to invoke the jsp and passing the request attributes, I am getting an error. Can you plz advise?

 

<sly data-sly-use.category="com.test.testpkg.aem.core.models.Settings"
data-sly-include="{'test.jsp' @ requestAttributes=category.settings}"></sly>

 

Error Details:
com.adobe.cq.sightly.WCMScriptHelper Failed to locate script {'test.jsp' @ requestAttributes=category.settings}

Avatar

Community Advisor

Hi @v1101 

 

Can you confirm test.jsp is present in the same folder where you are including 

<sly data-sly-use.category="com.test.testpkg.aem.core.models.Settings"
data-sly-include="{'test.jsp' @ requestAttributes=category.settings}"></sly> in HTL/Sightly HTML file.

 

test.jsp is relative path, so it expects test.jsp in the same folder. If the file is not present in same folder, you will have to include file with absolute path.

 

Avatar

Level 5
Hi @nupujain - File is present in the same folder. I have already tried with absolute & relative path but no luck. I am still getting he same error.