3. What & Why Sling Models?
- Introduction to Sling Models
- Need and Essence of Sling Models
- Design Goals
Sling Model Annotations
- Annotations Usage
Sling Model Injectors
- Injectors available in 1.0.x and 1.1.x
- Custom Injectors
- Injectors Usage
Agenda
Usage of Sling Models with Sightly and JSP
- Sling Models dependencies
- How to use Sling Models with JSP ?
- How to use Sling Models with Sightly?
Demo
- Functionality Demo
Appendix and Q&A
- Appendix
- Q&A
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. • Saves from creating own adapters
• Context aware Dependency Injection Framework
• Do More with Less Code making Code Readable and Removing
Redundant (Boiler Plate) Code
• Highly Testable Code
- Sling Model Inject Annotations defines the dependency
contract
- Mock dependencies with tools like Mockito @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 Cont...
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 ResourceResolver resolver;
@SlingObject private SlingHttpServletRequest request;
}
Injects resource from given path
@ResourcePath(path="/path/to/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. • 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 name;
@ValueMapValue (name="jcr:title", optional=true)
String title;
Injector-specific Annotations (Since
Sling Models Impl 1.0.6)
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
18. • OOTB supported in AEM 6 and above with
org.apache.sling.models.api package already present in AEM
instance
• For other version 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>
Dependency
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
Dependency Cont…
20. • Sling Model class object can be used in JSP by either
- <c:set var ="mycomp" value="<%=
resource.adaptTo(SlingModelDemo.class)%>" />
Or by using
- <sling:adaptTo adaptable="${resource}"
adaptTo="com.slingmodels.core. SlingModelDemo"
var=“mycomp"/>
Sling Models with JSP
21. • 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.mycomp="
com.slingmodels.core.SlingModelDemo">
Sling Models with Sightly