sling models using sightly and jsp by deepak khetawat

26
Sling Models Using Sightly and JSP DEEPAK KHETAWAT

Upload: aem-hub

Post on 26-Jan-2017

3.311 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Sling Models Using Sightly and JSP by Deepak Khetawat

Sling Models Using Sightly and JSP

DEEPAK KHETAWAT

Page 2: Sling Models Using Sightly and JSP by Deepak Khetawat

About the Speaker

Page 3: Sling Models Using Sightly and JSP by Deepak Khetawat

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

Page 4: Sling Models Using Sightly and JSP by Deepak Khetawat

What & Why Sling Models

Page 5: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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?

Page 6: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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

Page 7: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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 ?

Page 8: Sling Models Using Sightly and JSP by Deepak Khetawat

Sling Models Annotations

Page 9: Sling Models Using Sightly and JSP by Deepak Khetawat

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 .

Page 10: Sling Models Using Sightly and JSP by Deepak Khetawat

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)

Page 11: Sling Models Using Sightly and JSP by Deepak Khetawat

Sling Model Injectors

Page 12: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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 ?

Page 13: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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)

Page 14: Sling Models Using Sightly and JSP by Deepak Khetawat

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)

Page 15: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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)

AnnotationSupported Optional

ElementsInjector

@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

@ResourcePathoptional, path,

and name

resource-path

@OSGiService optional, filter osgi-services

@Self optional self

@SlingObject optional sling-object

Page 16: Sling Models Using Sightly and JSP by Deepak Khetawat

Injectors depicted in Felix Console

Page 17: Sling Models Using Sightly and JSP by Deepak Khetawat

Using Sling Models with Sightly and JSP

Page 18: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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

Page 19: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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…

Page 20: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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

Page 21: Sling Models Using Sightly and JSP by Deepak Khetawat

• 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

Page 22: Sling Models Using Sightly and JSP by Deepak Khetawat

Demo

Page 24: Sling Models Using Sightly and JSP by Deepak Khetawat

Appendix and Q&A

Page 25: Sling Models Using Sightly and JSP by Deepak Khetawat

• https://sling.apache.org/documentation/bundles/models.html

• Google ..

Appendix

Page 26: Sling Models Using Sightly and JSP by Deepak Khetawat

For more information contact:

DEEPAK KHETAWAT

M +91-9910941818

[email protected]

Thank you