Expand my Community achievements bar.

SOLVED

Sightly : request scope variable

Avatar

Level 2

I have to use below mentioned ProductUse class in 6 different components in a same template.

<sly data-sly-use.productUseBean="com.training.use.ProductUse">

As of now i am defining this all the components. 

How to define it once in template level and use it for different components?

 

Thanks in advance,

Divya

1 Accepted Solution

Avatar

Correct answer by
Employee

What is you follow this?

https://github.com/heervisscher/htl-examples/blob/master/core/src/main/java/com/adobe/examples/htl/c...

Then you can have this in your component:

<div data-sly-use.x="com.adobe.examples.htl.core.requestscope.RequestScope"></div>
${x.hello}

View solution in original post

8 Replies

Avatar

Level 9

Hi,

Your template must have inherited some other base component. Why can't you create a simple file and override it at your template level. This simple file can have Sightly statement and it would be initialized once. 

Above statement will enable to initialize your object once but you need to set this object in request scope as well.

Good article http://aempodcast.com/2015/sightly/emulating-requestscope-variables-sightly-aem/#.WD6FcbJ97IU

divyasekar wrote...

I have to use below mentioned ProductUse class in 6 different components in a same template.

<sly data-sly-use.productUseBean="com.training.use.ProductUse">

As of now i am defining this all the components. 

How to define it once in template level and use it for different components?

 

Thanks in advance,

Divya

 

Avatar

Employee

Or you can use data-sly-include to include a shared file.

Avatar

Level 2

Thank you for your response.

I should not use any shared file. I have to use request.setAttribute and getAttribute here. How to achieve this.

 

My scenario is going to be:

I have ProductUse class and ProductBean.
In my components i use the following snippet.
<sly data-sly-use.productUseBean="com.training.use.ProductUse"></sly>
${productUseBean.productBean.productTitle}

My achievement should be:

request.setAttribute should be used in productUse class and request.getAttribute should be used in my template.

<sly data-sly-use.productUseBean="com.training.use.ProductUse"></sly> this line should be avoided my components.

could you please tell me how to achieve this.

Avatar

Employee

You can do this via WCMUsePojo, there you have access to the request object,

Avatar

Level 2

I have set productBean object in use class using request.setAttribute. But i am not able to get request attribute in a component

Avatar

Employee

Is this not working?

setAttribute("x", yourBean);

In your component

${x.yourMethod}

Avatar

Correct answer by
Employee

What is you follow this?

https://github.com/heervisscher/htl-examples/blob/master/core/src/main/java/com/adobe/examples/htl/c...

Then you can have this in your component:

<div data-sly-use.x="com.adobe.examples.htl.core.requestscope.RequestScope"></div>
${x.hello}