Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Sling Models Using Sightly
and JSP
DEEPAK KHETAWAT
About the Speaker
What & Why Sling Models?
- Introduction to Sling Models
- Need and Essence of Sling Models
- Design Goals
Sling Model Anno...
What & Why Sling Models
• Sling Models are POJO’s which are automatically
mapped from Sling objects, typically resources, request
objects. Sometim...
• Entirely annotation driven. "Pure" POJOs
• Adapt multiple objects
• Work with existing Sling infrastructure
• Client doe...
• Saves from creating own adapters
• Context aware Dependency Injection Framework
• Do More with Less Code making Code Rea...
Sling Models Annotations
Annotations Usage
Sling Model Annotation Code Snippet Description
@Model @Model(adaptables = Resource.class) Class is anno...
Annotations Usage Cont...
Sling Model Annotation Code Snippet Description
@Named @Inject @Named("title") private String pa...
Sling Model Injectors
• Injectors are OSGi services implementing the
org.apache.sling.models.spi.Injector interface
• Injectors are invoked in o...
• Script Bindings (script-bindings)
• Value Map (valuemap)
• Child Resources (child-resources)
• Request Attributes (reque...
Injects adaptable itself or an object that can be adapted from
adaptable
@Self privateResource resource;
@Model(adaptables...
• Can use customized annotations with following advantages over
using the standard annotations:
- Less code to write (only...
Injectors depicted in Felix Console
Using Sling Models with Sightly and JSP
• OOTB supported in AEM 6 and above with
org.apache.sling.models.api package already present in AEM
instance
• For other v...
• Search for maven-bundle-plugin in pom.xml file , update it with
following
- <Sling-Model-Packages> com.slingmodels.core ...
• Sling Model class object can be used in JSP by either
- <c:set var ="mycomp" value="<%=
resource.adaptTo(SlingModelDemo....
• Sightly is a beautiful mark up language providing advantages like
separation of concerns , prevents xss vulnerabilities ...
Demo
Code can be found at:
https://github.com/deepakkhetawat/SlingModel-Demo.git
Where is the Code
Appendix and Q&A
• https://sling.apache.org/documentation/bundles/models.html
• Google ..
Appendix
For more information contact:
DEEPAK KHETAWAT
M +91-9910941818
deepak.khetawat@tothenew.com
Thank you
Prochain SlideShare
Chargement dans…5
×

Sling Models Using Sightly and JSP by Deepak Khetawat

6 143 vues

Publié le

Sling Models Using Sightly and JSP by Deepak Khetawat

Publié dans : Technologie
  • Soyez le premier à commenter

Sling Models Using Sightly and JSP by Deepak Khetawat

  1. 1. Sling Models Using Sightly and JSP DEEPAK KHETAWAT
  2. 2. About the Speaker
  3. 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
  4. 4. What & Why Sling Models
  5. 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. 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. 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 ?
  8. 8. Sling Models Annotations
  9. 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. 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)
  11. 11. Sling Model Injectors
  12. 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. 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. 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. 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
  16. 16. Injectors depicted in Felix Console
  17. 17. Using Sling Models with Sightly and JSP
  18. 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. 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. 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. 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
  22. 22. Demo
  23. 23. Code can be found at: https://github.com/deepakkhetawat/SlingModel-Demo.git Where is the Code
  24. 24. Appendix and Q&A
  25. 25. • https://sling.apache.org/documentation/bundles/models.html • Google .. Appendix
  26. 26. For more information contact: DEEPAK KHETAWAT M +91-9910941818 deepak.khetawat@tothenew.com Thank you

×