Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Handling JSP expressions in HTL/Sling Model

Avatar

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile
v1101
Level 2

02-12-2020

Hello Community - We have a JSP based template which needs to be converted into HTL. Currently we have planned to move all the business logic in Sling Model. Can you advise the best approach to handle the below in HTL.

 

A) <c:set var="runMode" value="uat" scope="request"/> - We can use <div data-sly-test.runMode="uat"> to assign a variable name, which can later be used like <div>${runMode}</div>. If the assignment is for few variables, we can use this approach, but this is not a best approach. Is there any better solution to handle this ?

B) request.setAttribute("isProductActive", isProductActive); - In the current implementation, we are setting the values in request. How can we handle this? If this has to be done in Sling model. How do we use this in HTL.
C) pageContext.setAttribute("fetchData",fetchData); - This is also required to handle in HTL.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

190 likes

Total Posts

199 posts

Correct Reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

190 likes

Total Posts

199 posts

Correct Reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

02-12-2020

Hi @v1101,

 

Here is what you can do:

 

A. If the run mode that you are setting is STATIC, you can create a new string variable in the sling model and access it in HTL.

Java:

private String runMode;

HTL:

<div data-sly-test.runMode="${object.runMode}">

Also, you can let author select the value via dialog.

 

You can also get it via slingSettingsService object:

slingSettingsService.getRunModes();

 

B. In sling models request attribute can be set using the request object.

Set the model adaptable to SlingHttpServletRequest

@Model(adaptables = SlingHttpServletRequest.class)

 

Inject the request:

@Inject
SlingHttpServletRequest request;

 

Using this request attribute, you can set the value in any attribute you need and send it to HTL.

 

C.  The pageContext object can be used to set, get or remove the attribute from the page, request, session, application.

 

You can set the context in sightly like this :

${properties.nodename @ context='html'}

 

Not sure why you need pageContext here, but here is the list of available global objects in HTL: https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/global-objects.html?lang=en...

 

Thanks,

Kiran Vedantam.

Answers (0)