Expand my Community achievements bar.

How to use POJO in Sling taglib?

Avatar

Level 3

Hi,

  I am trying to use POJO in sling taglib in CQ5 component.

 First, I created a component, called "myDataModel"

<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:Component" jcr:title="My Data Model Component" sling:resourceSuperType="foundation/components/parbase" allowedChildren="[*/*parsys]" allowedParents="[*/*parsys]" componentGroup="My Custom"/>

Then I have a dialog with a textfield named "firstname", this is the my dialog.xml

<?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" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" jcr:primaryType="cq:Dialog" xtype="dialog"> <items jcr:primaryType="cq:Widget" xtype="tabpanel"> <items jcr:primaryType="cq:WidgetCollection"> <tab1 jcr:primaryType="cq:Panel" border="false" height="" title="Tab 1" width=""> <items jcr:primaryType="nt:unstructured" fieldLabel="First Name" name="./firstname" validateOnBlur="true" xtype="textfield"/> </tab1> </items> </items> </jcr:root>

Then I create my POJO model class

package apps.mytestweb.components; import javax.inject.Inject; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; @Model(adaptables=Resource.class) public class MyDataModel { @Inject private String firstname; public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } }

in myDataModel.jsp page, this is how I wrote:

<%@include file="/libs/foundation/global.jsp"%> <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %> <sling adaptable="${resource}" adaptTo="apps.mytestweb.components.MyDataModel" var="mydbModel" /> This is my data model class. <c:out value="${mydbModel.firstname}" /> <br />

Nothing showed up.. and I have no idea what is the error...

Please help.

 

Thank you

 

Rui

14 Replies