SlideShare a Scribd company logo
1 of 29
Download to read offline
Sling Models Using Sightly
and JSP
DEEPAK KHETAWAT
About the Speaker
What & Why Sling Models
- What are Sling Models
- Design Goals
- Why Sling Models ?
Sling Model Annotations
- Annotations Usage
Sling Model Injectors
- What are Injectors
- Injectors available in 1.0.x and 1.1.x
- Injector-specific Annotations
Agenda
Usage of Sling Models
- Prerequisite
- How to use Sling Models with JSP ?
- How to use Sling Models with Sightly?
Demo
- Functionality Demo
Appendix and Q&A
- Appendix
- Q&A
What & Why Sling Models
• Sling Models are POJO’s which are automatically
mapped from Sling objects, typically resources, request
objects. Sometimes these POJOs need OSGi services
as well
What are Sling Models?
• Entirely annotation driven. "Pure" POJOs
• Adapt multiple objects
• Work with existing Sling infrastructure
• Client doesn't know/care that these objects are different than any
other adapter factory
• Support both classes and interfaces
• Pluggable
Design Goals
Development :
• Saves from creating own adapters
• Situation Based Dependency Injection Framework
• Do More with less code making code readable and Removing
redundant (boilerplate) code
Testing :
• Highly Testable Code
- Sling Model Inject Annotations defines binding to
dependencies
- Mock dependencies with tools like Mockito using
@InjectMocks
Why Sling Models ?
Sling Model Annotations
Annotations Usage
Sling Model Annotation Code Snippet Description
@Model @Model(adaptables = Resource.class) Class is annotated with @Model and the adaptable class
@Inject
@Inject private String propertyName; (class )
@Inject String getPropertyName(); (interface )
A property named "propertyName" will be looked up from the
Resource (after first adapting it to a ValueMap) and it is injected ,
else will return null if property not found .
@Default @Inject @Default(values = "AEM") private String technology; A default value (for Strings , Arrays , primitives etc. )
@Optional @Inject @Optional private String otherName.
@Injected fields/methods are assumed to be required. To mark
them as optional, use @Optional i.e resource or adaptable may or
may not have property .
Annotations Usage
Sling Model Annotation Code Snippet Description
@Named @Inject @Named("title") private String pageTitle;
Inject a property whose name does NOT match the Model field
name
@Via
@Model(adaptables=SlingHttpServletRequest.class)
public interface SlingModelDemo {
@Inject @Via("resource") String getPropertyName(); }
Use a JavaBean property of the adaptable as the source of the
injection
// Code Snippet will return
request.getResource().adaptTo(ValueMap.class).get("propertyNam
e", String.class)
@Source
@Model(adaptables=SlingHttpServletRequest.class)
@Inject @Source("script-bindings") Resource
getResource(); }
Explicitly tie an injected field or method to a particular injector (by
name). Can also be on other annotations
//Code snippet will ensure that "resource" is retrieved from the
bindings, not a request attribute
@PostConstruct
@PostConstruct
protected void sayHello() { logger.info("hello"); }
Methods to call upon model option creation (only for model classes)
Sling Model Injectors
• Injectors are OSGi services implementing the
org.apache.sling.models.spi.Injector interface
• Injectors are invoked in order of their service ranking, from lowest to
highest.
What are Injectors ?
• Script Bindings (script-bindings)
• Value Map (valuemap)
• Child Resources (child-resources)
• Request Attributes (request-attributes)
• OSGI Service References (osgi-services)
Standard Injectors in 1.0.x
Injectors (with @Source names)
Injects adaptable itself or an object that can be adapted from
adaptable
@Self privateResource resource;
@Model(adaptables = SlingHttpServletRequest.class)
public class SlingModelDemo {
@SlingObject private Resource resource;
@SlingObject private SlingHttpServletRequest request;
}
Injects resource from given path , applicable to Resource or
SlingHttpServletRequest objects
@ResourcePath(path="/resource-path") private Resource resource;
• Self Injector (self)
• Sling Object Injector (sling-object)
• Resource Path Injector (resource-path)
Standard Injectors in 1.1.x
Injectors (with @Source names)
• Supported since Sling Models Impl 1.0.6
• Can use customized annotations with following advantages over
using the standard annotations:
- Less code to write (only one annotation is necessary in most of
the cases)
- More robust
• Injector Specific Annotation specifies source explicitly
• Example
@ValueMapValue String propertyName;
@ValueMapValue (name="jcr:title", optional=true)
String title;
Injector-specific Annotations
Annotation
Supported Optional
Elements
Injector
@ScriptVariable optional and name script-bindings
@ValueMapValue optional, name and via valuemap
@ChildResource optional, name and via child-resources
@RequestAttribute optional, name and via request-attributes
@ResourcePath
optional, path,
and name
resource-path
@OSGiService optional, filter osgi-services
@Self optional self
@SlingObject optional sling-object
Injectors depicted in Felix Console
Using Sling Models with Sightly and JSP
• Need org.apache.sling.models.api package
- OOTB supported in AEM 6 and above version , with
org.apache.sling.models.api package already present in AEM
instance
- For other versions download package from
http://sling.apache.org/downloads.cgi and install in AEM instance
• Maven dependency can be found at
http://<host>:<port>/system/console/depfinder , search for
org.apache.sling.models.annotations.Model
<dependency> <groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.0.0</version> <scope>provided</scope>
</dependency>
Prerequisite
• Search for maven-bundle-plugin in pom.xml file , update it with
following
- <Sling-Model-Packages> com.slingmodels.core </Sling-Model-
Packages>
- It is because for Sling Model classes to be picked up this header
must be added to the bundle's manifest
Prerequisite
• Supported as of Sling Models 1.1.0
• Name of a constructor argument parameter cannot be detected
via the Java Reflection API
• @Named annotation is mandatory for injectors that require a
name for resolving the injection
• @Model(adaptables=Resource.class)
public class MyModel {
@Inject public MyModel(@Named("propertyName") String
propertyName)
{ // constructor code }
}
Constructor Injections
• Sling Model class object can be used in JSP by either
- <c:set var = "slingModel" value="<%=
resource.adaptTo(SlingModelDemo.class)%>" />
Or
- <sling:adaptTo adaptable="${resource}"
adaptTo="com.slingmodels.core.SlingModelDemo"
var=“slingModel"/>
Sling Models with JSP
• Sightly is a beautiful mark up language providing advantages like
separation of concerns , prevents xss vulnerabilities .
• Sling Model class object can be used in Sightly by :
<div data-sly-use.slingModel ="
com.slingmodels.core.SlingModelDemo">
Sling Models with Sightly
Demo
• Retrieving dialog values of a component
• Retrieving recent pages/articles under a content path
• Many more 
Groovifying Demo UseCases :
Code can be found at:
https://github.com/deepakkhetawat/Sling-Model-Demo.git
Where is the Code ?
Appendix and Q&A
• https://sling.apache.org/documentation/bundles/models.html
• http://slideshare.net/justinedelson/sling-models-overview
• https://sling.apache.org/documentation/bundles/sling-scripting-jsp-
taglib.html
Appendix
Q&A
For more information contact:
DEEPAK KHETAWAT
M +91-9910941818
deepak.khetawat@tothenew.com
Thank you

More Related Content

What's hot

RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSNeil Ghosh
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes WorkshopErik Hatcher
 
Solr Application Development Tutorial
Solr Application Development TutorialSolr Application Development Tutorial
Solr Application Development TutorialErik Hatcher
 
eZ Find workshop: advanced insights & recipes
eZ Find workshop: advanced insights & recipeseZ Find workshop: advanced insights & recipes
eZ Find workshop: advanced insights & recipesPaul Borgermans
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conferenceErik Hatcher
 
Introduction Apache Solr & PHP
Introduction Apache Solr & PHPIntroduction Apache Solr & PHP
Introduction Apache Solr & PHPHiraq Citra M
 
Apache Solr-Webinar
Apache Solr-WebinarApache Solr-Webinar
Apache Solr-WebinarEdureka!
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 
Consuming External Content and Enriching Content with Apache Camel
Consuming External Content and Enriching Content with Apache CamelConsuming External Content and Enriching Content with Apache Camel
Consuming External Content and Enriching Content with Apache Cameltherealgaston
 
Restful web services with java
Restful web services with javaRestful web services with java
Restful web services with javaVinay Gopinath
 
Introduction to Apache solr
Introduction to Apache solrIntroduction to Apache solr
Introduction to Apache solrKnoldus Inc.
 
Using Apache Solr
Using Apache SolrUsing Apache Solr
Using Apache Solrpittaya
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSCarol McDonald
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)Erik Hatcher
 
Do you need an external search platform for Adobe Experience Manager?
Do you need an external search platform for Adobe Experience Manager?Do you need an external search platform for Adobe Experience Manager?
Do you need an external search platform for Adobe Experience Manager?therealgaston
 

What's hot (20)

RestFull Webservices with JAX-RS
RestFull Webservices with JAX-RSRestFull Webservices with JAX-RS
RestFull Webservices with JAX-RS
 
Apache Solr Workshop
Apache Solr WorkshopApache Solr Workshop
Apache Solr Workshop
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
 
Solr Application Development Tutorial
Solr Application Development TutorialSolr Application Development Tutorial
Solr Application Development Tutorial
 
eZ Find workshop: advanced insights & recipes
eZ Find workshop: advanced insights & recipeseZ Find workshop: advanced insights & recipes
eZ Find workshop: advanced insights & recipes
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conference
 
Introduction Apache Solr & PHP
Introduction Apache Solr & PHPIntroduction Apache Solr & PHP
Introduction Apache Solr & PHP
 
Apache Solr-Webinar
Apache Solr-WebinarApache Solr-Webinar
Apache Solr-Webinar
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 
Consuming External Content and Enriching Content with Apache Camel
Consuming External Content and Enriching Content with Apache CamelConsuming External Content and Enriching Content with Apache Camel
Consuming External Content and Enriching Content with Apache Camel
 
Restful web services with java
Restful web services with javaRestful web services with java
Restful web services with java
 
Introduction to Apache solr
Introduction to Apache solrIntroduction to Apache solr
Introduction to Apache solr
 
Using Apache Solr
Using Apache SolrUsing Apache Solr
Using Apache Solr
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
RESTing with JAX-RS
RESTing with JAX-RSRESTing with JAX-RS
RESTing with JAX-RS
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
 
Do you need an external search platform for Adobe Experience Manager?
Do you need an external search platform for Adobe Experience Manager?Do you need an external search platform for Adobe Experience Manager?
Do you need an external search platform for Adobe Experience Manager?
 

Viewers also liked

Apache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTApache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTCarsten Ziegeler
 
Rapid RESTful Web Applications with Apache Sling and Jackrabbit
Rapid RESTful Web Applications with Apache Sling and JackrabbitRapid RESTful Web Applications with Apache Sling and Jackrabbit
Rapid RESTful Web Applications with Apache Sling and JackrabbitCraig Dickson
 
Effective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingEffective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingRobert Munteanu
 
Of microservices and microservices
Of microservices and microservicesOf microservices and microservices
Of microservices and microservicesRobert Munteanu
 
Apache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middlewareApache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middlewareRobert Munteanu
 
Apache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayApache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayRobert Munteanu
 
Barrett_Rubin essay
Barrett_Rubin essayBarrett_Rubin essay
Barrett_Rubin essayBill Barrett
 
Barboza rizo tarea_secion6
Barboza rizo tarea_secion6Barboza rizo tarea_secion6
Barboza rizo tarea_secion6dianahbetzabehb
 
Anti Corruption
Anti CorruptionAnti Corruption
Anti CorruptionMayowa Oni
 
What's cool in the new and updated OSGi Specs
What's cool in the new and updated OSGi SpecsWhat's cool in the new and updated OSGi Specs
What's cool in the new and updated OSGi SpecsCarsten Ziegeler
 
Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Carsten Ziegeler
 
What's cool in the new and updated OSGi specs
What's cool in the new and updated OSGi specsWhat's cool in the new and updated OSGi specs
What's cool in the new and updated OSGi specsCarsten Ziegeler
 
Monitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleMonitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleCarsten Ziegeler
 
Distributed Eventing in OSGi
Distributed Eventing in OSGiDistributed Eventing in OSGi
Distributed Eventing in OSGiCarsten Ziegeler
 
Diabetes and its oral complication
Diabetes and its oral complicationDiabetes and its oral complication
Diabetes and its oral complicationDr. Monali Prajapati
 
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)Carsten Ziegeler
 
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM Communities
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM CommunitiesMongoDB Days Silicon Valley: Using MongoDB with Adobe AEM Communities
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM CommunitiesMongoDB
 

Viewers also liked (19)

Apache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTApache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and REST
 
Rapid RESTful Web Applications with Apache Sling and Jackrabbit
Rapid RESTful Web Applications with Apache Sling and JackrabbitRapid RESTful Web Applications with Apache Sling and Jackrabbit
Rapid RESTful Web Applications with Apache Sling and Jackrabbit
 
Effective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingEffective Web Application Development with Apache Sling
Effective Web Application Development with Apache Sling
 
Of microservices and microservices
Of microservices and microservicesOf microservices and microservices
Of microservices and microservices
 
Apache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middlewareApache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middleware
 
Apache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayApache Sling as a Microservices Gateway
Apache Sling as a Microservices Gateway
 
Barrett_Rubin essay
Barrett_Rubin essayBarrett_Rubin essay
Barrett_Rubin essay
 
Barboza rizo tarea_secion6
Barboza rizo tarea_secion6Barboza rizo tarea_secion6
Barboza rizo tarea_secion6
 
A fazer
A fazerA fazer
A fazer
 
Anti Corruption
Anti CorruptionAnti Corruption
Anti Corruption
 
Resume
ResumeResume
Resume
 
What's cool in the new and updated OSGi Specs
What's cool in the new and updated OSGi SpecsWhat's cool in the new and updated OSGi Specs
What's cool in the new and updated OSGi Specs
 
Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)
 
What's cool in the new and updated OSGi specs
What's cool in the new and updated OSGi specsWhat's cool in the new and updated OSGi specs
What's cool in the new and updated OSGi specs
 
Monitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleMonitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web Console
 
Distributed Eventing in OSGi
Distributed Eventing in OSGiDistributed Eventing in OSGi
Distributed Eventing in OSGi
 
Diabetes and its oral complication
Diabetes and its oral complicationDiabetes and its oral complication
Diabetes and its oral complication
 
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)
Apache Sling - Distributed Eventing, Discovery, and Jobs (adaptTo 2013)
 
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM Communities
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM CommunitiesMongoDB Days Silicon Valley: Using MongoDB with Adobe AEM Communities
MongoDB Days Silicon Valley: Using MongoDB with Adobe AEM Communities
 

Similar to Using Sling Models with Sightly and JSP

Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatAEM HUB
 
Understanding Sling Models in AEM
Understanding Sling Models in AEMUnderstanding Sling Models in AEM
Understanding Sling Models in AEMAccunity Software
 
Sling models by Justin Edelson
Sling models by Justin Edelson Sling models by Justin Edelson
Sling models by Justin Edelson AEM HUB
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags AdvancedAkramWaseem
 
13 java beans
13 java beans13 java beans
13 java beanssnopteck
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and SlingLo Ki
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and SlingLokesh BS
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźAEM HUB
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.jsIvano Malavolta
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 
070517 Jena
070517 Jena070517 Jena
070517 Jenayuhana
 
Section 7 fundamentals
Section 7   fundamentalsSection 7   fundamentals
Section 7 fundamentalsJuarez Junior
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsSadayuki Furuhashi
 

Similar to Using Sling Models with Sightly and JSP (20)

Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak Khetawat
 
slingmodels
slingmodelsslingmodels
slingmodels
 
Understanding Sling Models in AEM
Understanding Sling Models in AEMUnderstanding Sling Models in AEM
Understanding Sling Models in AEM
 
Sling models by Justin Edelson
Sling models by Justin Edelson Sling models by Justin Edelson
Sling models by Justin Edelson
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
 
13 java beans
13 java beans13 java beans
13 java beans
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and Sling
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and Sling
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
When Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz NiedźwiedźWhen Sightly Meets Slice by Tomasz Niedźwiedź
When Sightly Meets Slice by Tomasz Niedźwiedź
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.js
 
Osgi
OsgiOsgi
Osgi
 
Handlebars & Require JS
Handlebars  & Require JSHandlebars  & Require JS
Handlebars & Require JS
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
Sling Models Overview
Sling Models OverviewSling Models Overview
Sling Models Overview
 
Prototype-1
Prototype-1Prototype-1
Prototype-1
 
Prototype-1
Prototype-1Prototype-1
Prototype-1
 
070517 Jena
070517 Jena070517 Jena
070517 Jena
 
Section 7 fundamentals
Section 7   fundamentalsSection 7   fundamentals
Section 7 fundamentals
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGems
 

Recently uploaded

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 

Recently uploaded (20)

Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 

Using Sling Models with Sightly and JSP

  • 1. Sling Models Using Sightly and JSP DEEPAK KHETAWAT
  • 3. What & Why Sling Models - What are Sling Models - Design Goals - Why Sling Models ? Sling Model Annotations - Annotations Usage Sling Model Injectors - What are Injectors - Injectors available in 1.0.x and 1.1.x - Injector-specific Annotations Agenda Usage of Sling Models - Prerequisite - How to use Sling Models with JSP ? - How to use Sling Models with Sightly? Demo - Functionality Demo Appendix and Q&A - Appendix - Q&A
  • 4. What & Why Sling Models
  • 5. • Sling Models are POJO’s which are automatically mapped from Sling objects, typically resources, request objects. Sometimes these POJOs need OSGi services as well What are Sling Models?
  • 6. • Entirely annotation driven. "Pure" POJOs • Adapt multiple objects • Work with existing Sling infrastructure • Client doesn't know/care that these objects are different than any other adapter factory • Support both classes and interfaces • Pluggable Design Goals
  • 7. Development : • Saves from creating own adapters • Situation Based Dependency Injection Framework • Do More with less code making code readable and Removing redundant (boilerplate) code Testing : • Highly Testable Code - Sling Model Inject Annotations defines binding to dependencies - Mock dependencies with tools like Mockito using @InjectMocks Why Sling Models ?
  • 9. Annotations Usage Sling Model Annotation Code Snippet Description @Model @Model(adaptables = Resource.class) Class is annotated with @Model and the adaptable class @Inject @Inject private String propertyName; (class ) @Inject String getPropertyName(); (interface ) A property named "propertyName" will be looked up from the Resource (after first adapting it to a ValueMap) and it is injected , else will return null if property not found . @Default @Inject @Default(values = "AEM") private String technology; A default value (for Strings , Arrays , primitives etc. ) @Optional @Inject @Optional private String otherName. @Injected fields/methods are assumed to be required. To mark them as optional, use @Optional i.e resource or adaptable may or may not have property .
  • 10. Annotations Usage Sling Model Annotation Code Snippet Description @Named @Inject @Named("title") private String pageTitle; Inject a property whose name does NOT match the Model field name @Via @Model(adaptables=SlingHttpServletRequest.class) public interface SlingModelDemo { @Inject @Via("resource") String getPropertyName(); } Use a JavaBean property of the adaptable as the source of the injection // Code Snippet will return request.getResource().adaptTo(ValueMap.class).get("propertyNam e", String.class) @Source @Model(adaptables=SlingHttpServletRequest.class) @Inject @Source("script-bindings") Resource getResource(); } Explicitly tie an injected field or method to a particular injector (by name). Can also be on other annotations //Code snippet will ensure that "resource" is retrieved from the bindings, not a request attribute @PostConstruct @PostConstruct protected void sayHello() { logger.info("hello"); } Methods to call upon model option creation (only for model classes)
  • 12. • Injectors are OSGi services implementing the org.apache.sling.models.spi.Injector interface • Injectors are invoked in order of their service ranking, from lowest to highest. What are Injectors ?
  • 13. • Script Bindings (script-bindings) • Value Map (valuemap) • Child Resources (child-resources) • Request Attributes (request-attributes) • OSGI Service References (osgi-services) Standard Injectors in 1.0.x Injectors (with @Source names)
  • 14. Injects adaptable itself or an object that can be adapted from adaptable @Self privateResource resource; @Model(adaptables = SlingHttpServletRequest.class) public class SlingModelDemo { @SlingObject private Resource resource; @SlingObject private SlingHttpServletRequest request; } Injects resource from given path , applicable to Resource or SlingHttpServletRequest objects @ResourcePath(path="/resource-path") private Resource resource; • Self Injector (self) • Sling Object Injector (sling-object) • Resource Path Injector (resource-path) Standard Injectors in 1.1.x Injectors (with @Source names)
  • 15. • Supported since Sling Models Impl 1.0.6 • Can use customized annotations with following advantages over using the standard annotations: - Less code to write (only one annotation is necessary in most of the cases) - More robust • Injector Specific Annotation specifies source explicitly • Example @ValueMapValue String propertyName; @ValueMapValue (name="jcr:title", optional=true) String title; Injector-specific Annotations Annotation Supported Optional Elements Injector @ScriptVariable optional and name script-bindings @ValueMapValue optional, name and via valuemap @ChildResource optional, name and via child-resources @RequestAttribute optional, name and via request-attributes @ResourcePath optional, path, and name resource-path @OSGiService optional, filter osgi-services @Self optional self @SlingObject optional sling-object
  • 16. Injectors depicted in Felix Console
  • 17. Using Sling Models with Sightly and JSP
  • 18. • Need org.apache.sling.models.api package - OOTB supported in AEM 6 and above version , with org.apache.sling.models.api package already present in AEM instance - For other versions download package from http://sling.apache.org/downloads.cgi and install in AEM instance • Maven dependency can be found at http://<host>:<port>/system/console/depfinder , search for org.apache.sling.models.annotations.Model <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.models.api</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> Prerequisite
  • 19. • Search for maven-bundle-plugin in pom.xml file , update it with following - <Sling-Model-Packages> com.slingmodels.core </Sling-Model- Packages> - It is because for Sling Model classes to be picked up this header must be added to the bundle's manifest Prerequisite
  • 20. • Supported as of Sling Models 1.1.0 • Name of a constructor argument parameter cannot be detected via the Java Reflection API • @Named annotation is mandatory for injectors that require a name for resolving the injection • @Model(adaptables=Resource.class) public class MyModel { @Inject public MyModel(@Named("propertyName") String propertyName) { // constructor code } } Constructor Injections
  • 21. • Sling Model class object can be used in JSP by either - <c:set var = "slingModel" value="<%= resource.adaptTo(SlingModelDemo.class)%>" /> Or - <sling:adaptTo adaptable="${resource}" adaptTo="com.slingmodels.core.SlingModelDemo" var=“slingModel"/> Sling Models with JSP
  • 22. • Sightly is a beautiful mark up language providing advantages like separation of concerns , prevents xss vulnerabilities . • Sling Model class object can be used in Sightly by : <div data-sly-use.slingModel =" com.slingmodels.core.SlingModelDemo"> Sling Models with Sightly
  • 23. Demo
  • 24. • Retrieving dialog values of a component • Retrieving recent pages/articles under a content path • Many more  Groovifying Demo UseCases :
  • 25. Code can be found at: https://github.com/deepakkhetawat/Sling-Model-Demo.git Where is the Code ?
  • 27. • https://sling.apache.org/documentation/bundles/models.html • http://slideshare.net/justinedelson/sling-models-overview • https://sling.apache.org/documentation/bundles/sling-scripting-jsp- taglib.html Appendix
  • 28. Q&A
  • 29. For more information contact: DEEPAK KHETAWAT M +91-9910941818 deepak.khetawat@tothenew.com Thank you