Tuesday, July 8, 2014

JCR content mapping with Sling Model

follow the easy steps to use sling model for JCR content mapping.

  1. Add sling model bundle dependency information in pom.xml
    <dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.models.api</artifactId>
    <version>1.0.0</version>
    </dependency>
    view raw pom.xml hosted with ❤ by GitHub
     
  2. Add Sling-Model-Packages declaration in the instruction 
    <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
    <instructions>
    <Sling-Model-Packages>
    edu.saral.models
    </Sling-Model-Packages>
    <Bundle-SymbolicName>edu.saral.slingmodel-bundle</Bundle-SymbolicName>
    </instructions>
    </configuration>
    </plugin>
    view raw declaration.xml hosted with ❤ by GitHub
  3. Annotate pojo class with @Model annotation and use @Inject against the fields (with getters)
    package edu.saral.models;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.models.annotations.Model;
    import javax.inject.Inject;
    /**
    * Created by behera on 07-07-2014.
    */
    @Model( adaptables = Resource.class )
    public class Person {
    @Inject private String firstName;
    @Inject private String lastName;
    @Inject private String email;
    @Inject private String mobile;
    view raw Person.java hosted with ❤ by GitHub
     
  4. Now adapt this pojo with resource.adaptTo() 
    person = getResource().adaptTo( Person.class );
    LOGGER.info( "person {} ", person );
    view raw PersonBean.java hosted with ❤ by GitHub
  5. This Will map the below content structure easily without the bulgy or cluttered code.  
    <?xml version="1.0" encoding="UTF-8"?>
    <jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Dialog"
    helpPath="en/cq/current/wcm/default_components.html#Title"
    title="Person"
    xtype="panel">
    <items jcr:primaryType="cq:WidgetCollection">
    <title
    jcr:primaryType="cq:Widget"
    fieldLabel="First Name"
    name="./firstName"
    xtype="textfield"/>
    <link
    jcr:primaryType="cq:Widget"
    fieldLabel="Last Name"
    name="./lastName"
    xtype="textfield"/>
    <email
    jcr:primaryType="cq:Widget"
    fieldLabel="Email"
    name="./email"
    xtype="textfield"/>
    <mobile
    jcr:primaryType="cq:Widget"
    fieldLabel="Mobile"
    name="./mobile"
    xtype="numberfield"/>
    </items>
    </jcr:root>
    view raw dialog.xml hosted with ❤ by GitHub


2 comments:

  1. Interesting ... But does it really help you to get rid of bulky codes? Now you don't have to write code for reading properties anymore that's it.
    What if a resource has children and subchildren (getting created at run time ). Are you going to create bean class for each child resource? How you are going to handle this scenario. Please provide a sample code for that...

    ReplyDelete
    Replies
    1. You can inject the child resources also in the form of list. Accordingly you have to design your model.

      Delete