Resolving Sling Model in Jsp files | Community
Skip to main content
Level 5
February 14, 2023
Solved

Resolving Sling Model in Jsp files

  • February 14, 2023
  • 4 replies
  • 3092 views

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

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 ChitraMadan

Hi @kevin_gta,

 

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/refactoring-tools/aem-modernization-tools.html?lang=en

 

Thanks,

Chitra

4 replies

ChitraMadan
Community Advisor
ChitraMadanCommunity AdvisorAccepted solution
Community Advisor
February 14, 2023

Hi @kevin_gta,

 

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/refactoring-tools/aem-modernization-tools.html?lang=en

 

Thanks,

Chitra

Kevin_GTaAuthor
Level 5
February 15, 2023

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

Umesh_Thakur
Community Advisor
Community Advisor
February 15, 2023

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 

Kevin_GTaAuthor
Level 5
February 15, 2023

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. 

 

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 15, 2023

@kevin_gta 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.

Kevin_GTaAuthor
Level 5
February 15, 2023

Thanks for the suggestion. I will try that out.

joerghoh
Adobe Employee
Adobe Employee
February 18, 2023

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.