follow the easy steps to use sling model for JCR content mapping.
- Add sling model bundle dependency information in pom.xml This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.models.api</artifactId> <version>1.0.0</version> </dependency> - Add Sling-Model-Packages declaration in the instruction This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<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> - Annotate pojo class with @Model annotation and use @Inject against the fields (with getters) This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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; - Now adapt this pojo with resource.adaptTo() This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
person = getResource().adaptTo( Person.class ); LOGGER.info( "person {} ", person ); - This Will map the below content structure easily without the bulgy or cluttered code. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<?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>
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.
ReplyDeleteWhat 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...
You can inject the child resources also in the form of list. Accordingly you have to design your model.
Delete