Expand my Community achievements bar.

SOLVED

Resolving Sling Model in Jsp files

Avatar

Level 7

Hi All,

 

I like to know if there is a way that jsp files can interpret Sightly just like html files. Let’s use the AEM demo HelloWorldModel

In my jsp file, this will work

 

<%@include file="/apps/mysite/components/global.jsp" %>

<%@page session="false" import=“mysite.core.models.HelloWorldModel"%>

<c:set var=“helloModel” value="<%= resource.adaptTo(HelloWorldModel.class)%>"/>

<div>Message : ${helloModel.message}</div> <!-- will see the message -->

 

In the helloworld component from AEM, which has a “.html” default extension, it also works

 

<p data-sly-test="${properties.text}">Text property: ${properties.text}</p>

<pre data-sly-use.hello="mysite.core.models.HelloWorldModel">

HelloWorldModel says:

${hello.message} <!-- will see the message -->

</pre>

 

But when I include the the helloworld model in the jsp file, it doesn’t work. I include it in one of my page template “.jsp” file,

 

<pre data-sly-use.hello="mysite.core.models.HelloWorldModel">

HelloWorldModel says:

${hello.message} <!-- can’t see the message -->

</pre>

 

It can’t interpret the sling model because jsp doesn’t understand the Sightly syntax. We have tons of jsp files, most of them use a lot of beans, which are equally the same as sling models. We may not be able to convert all the beans into sling models in phase one, so we may need to mix them together.

 

Thanks!

 

-kt

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @kevingtan,

 

You cannot include sightly tags in a jsp, however you can include a jsp component in sightly, for example

 

<sly data-sly-resource="${'par1' @ resourceType='projectname/components/content/foldername/componentname'}"/>

where projectname/components/content/foldername/componentname points to a JSP component.

In your case, I would suggest component by component migration to sightly and Sling Modals i.e if you're migrating 1 component, then change to both sightly and Sling Modal at the same time. You can still have mix of components with some written in jsp and some in sightly and Sling Modals.

 

You can also go through this tool for modernising your components - https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/migration-journey/r...

 

Thanks,

Chitra

View solution in original post

7 Replies

Avatar

Correct answer by
Community Advisor

Hi @kevingtan,

 

You cannot include sightly tags in a jsp, however you can include a jsp component in sightly, for example

 

<sly data-sly-resource="${'par1' @ resourceType='projectname/components/content/foldername/componentname'}"/>

where projectname/components/content/foldername/componentname points to a JSP component.

In your case, I would suggest component by component migration to sightly and Sling Modals i.e if you're migrating 1 component, then change to both sightly and Sling Modal at the same time. You can still have mix of components with some written in jsp and some in sightly and Sling Modals.

 

You can also go through this tool for modernising your components - https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/migration-journey/r...

 

Thanks,

Chitra

Avatar

Level 7

Thanks for your detailed suggestion. I will try that out.

Avatar

Community Advisor

It will not work if you use sightly tag in JSP.

I am not sure why are you using sling model in jsp, but I think you can call it thru adaptTo method.

Inside jsp's scriptlet you get the global object request or resource(it depends) adapt the sling model from it like:

 

HelloWorldModel helloWorldModel = resource.adaptTo(HelloWorldModel.class);

 

in the above snippet, Sling model was adaptables to Resource so I adapted it from resource but this depend on our use case.

 

Please see below link

https://aemhelper.blogspot.com/2021/08/common-myths-around-sling-model.html

 

Hope this helps

Umesh Thakur 

Avatar

Level 7

Hi @Umesh_Thakur ,

 

We have a lot of beans for page templates, not only components. For instance, event landing page template has a bean class associated with the front-end jsp file via a bean resolver, and there are a lot of jsp and jstl in the file. Some page templates are quite complicated such as including other static components as drawers, breadcrumb, CTA, etc.  I am trying to convert the bean to sling model, while reviving the jsp slowly. That's my purpose using sling model in jsp, I may have to wipe out the whole jsp file if that's not the way to go. 

 

Avatar

Community Advisor

@kevingtan if you want to be funky, you can reference a sightly HTL script, <%@include file="/apps/mysite/components/myhtml.html" %>, and from there, the myhtml.html should be referencing a sling model. That should work.

Avatar

Employee Advisor

The Sling Model documentation points out a few ways to include a sling model: https://sling.apache.org/documentation/bundles/models.html#client-code-1, and both the scriptlet way and the <sling:adaptTo> tag should work in the context of a JSP.