Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to use POJO in Sling taglib?

Avatar

Avatar
Validate 10
Level 2
Rui_J
Level 2

Likes

4 likes

Total Posts

39 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 10
Level 2
Rui_J
Level 2

Likes

4 likes

Total Posts

39 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
Rui_J
Level 2

15-10-2015

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

Replies

Avatar

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile
Sam205505050
Level 4

15-10-2015

is org.apache.sling.models.annotation class exists in cq instance or corresponding bundle is active?  

remove ",version=[1.0,2) " from your manifest file inside your bundle (change the bundle extension fron jar to zip for modifying manifest file) and then again change the bundle extension from zip to jar and upload it in felix console. 

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

15-10-2015

All back end OSGi bundles must be active for front-end components (ie - JSP) to communicate with them. When you have red like that -it means that there is a missing dependency - in this case - the version may be causing the issue. Remove the version from the MF file as stated in a previous answer.  

Avatar

Avatar
Validate 10
Level 2
Rui_J
Level 2

Likes

4 likes

Total Posts

39 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 10
Level 2
Rui_J
Level 2

Likes

4 likes

Total Posts

39 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
Rui_J
Level 2

15-10-2015

smacdonald2008 wrote...

All back end OSGi bundles must be active for front-end components (ie - JSP) to communicate with them. When you have red like that -it means that there is a missing dependency - in this case - the version may be causing the issue. Remove the version from the MF file as stated in a previous answer.  

 

  OK, after followed goffinf's link https://github.com/Adobe-Consulting-Services/com.adobe.acs.bundles.sling-models/releases/tag/com.ado...

  I have installed the sling model Bundle. Now I dont get any error from bundle web console.

  I guess I am getting close. But still I cannot output the  value. 

  This is how I called my Sling Model

<sling:defineObjects /> My Data Model <sling adaptable="${resource}" adaptTo="com.techsharp.mytestweb.model.MyDataModel" var="mydbModel" /> ${mydbModel.firstname} 

 

  this is bundle info.

 

mytestweb Bundlecom.techsharp.mytestweb-bundle Symbolic Name    com.techsharp.mytestweb-bundle Version 0.0.1.SNAPSHOT Bundle Location jcrinstall:/apps/mytestweb/install/mytestweb-bundle-0.0.1-SNAPSHOT.jar Last Modification Tue Jun 24 14:11:06 EDT 2014 Description Maven Multimodule project for mytestweb. Start Level 20 Exported Packages com.techsharp.mytestweb,version=1.0.0 com.techsharp.mytestweb.model,version=0.0.1.SNAPSHOT Imported Packages javax.inject,version=0.0.0 from org.apache.sling.models.api (322) javax.servlet,version=2.5.0 from cqse-httpservice (25) org.apache.sling.api,version=2.2.0 from org.apache.sling.api (123) org.apache.sling.api.request,version=2.3.0 from org.apache.sling.api (123) org.apache.sling.api.resource,version=2.3.2 from org.apache.sling.api (123) org.apache.sling.jcr.api,version=2.1.0 from org.apache.sling.jcr.api (74) org.apache.sling.models.annotations,version=1.0.0 from org.apache.sling.models.api (322) org.slf4j,version=1.6.4 from slf4j.api (14) Service ID 1950 Types: com.techsharp.mytestweb.HelloService Service PID: com.techsharp.mytestweb.impl.HelloServiceImpl Component Name: com.techsharp.mytestweb.impl.HelloServiceImpl Component ID: 2941 Service ID 1951 Types: javax.servlet.Filter Service PID: com.techsharp.mytestweb.impl.filters.LoggingFilter Component Name: com.techsharp.mytestweb.impl.filters.LoggingFilter Component ID: 2942 Manifest Headers Bnd-LastModified: 1403633464552 Build-Jdk: 1.7.0_51 Built-By: RuiJ Bundle-Description: Maven Multimodule project for mytestweb. Bundle-ManifestVersion: 2 Bundle-Name: mytestweb Bundle Bundle-SymbolicName: com.techsharp.mytestweb-bundle Bundle-Version: 0.0.1.SNAPSHOT Created-By: Apache Maven Bundle Plugin Export-Package: com.techsharp.mytestweb; version="1.0.0", com.techsharp.mytestweb.model; uses:="org.apache.sling.models.annotations, javax.inject, org.apache.sling.api.resource"; version="0.0.1.SNAPSHOT" Import-Package: com.techsharp.mytestweb; version="[1.0, 2)", javax.inject; version="[0.0, 1)", javax.servlet, org.apache.sling.api; version="[2.1, 3)", org.apache.sling.api.request; version="[2.1, 3)", org.apache.sling.api.resource; version="[2.1, 3)", org.apache.sling.jcr.api; version="[2.1, 3)", org.apache.sling.models.annotations; version="[1.0, 2)", org.slf4j; version="[1.5, 2)" Manifest-Version: 1.0 Service-Component: OSGI-INF/serviceComponents.xml Tool: Bnd-1.50.0 Used Services Service #13 of type(s) [org.osgi.service.log.LogService]

Avatar

Avatar
Boost 50
Employee
justin_at_adobe
Employee

Likes

76 likes

Total Posts

522 posts

Correct Reply

259 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 25
Boost 10
View profile

Avatar
Boost 50
Employee
justin_at_adobe
Employee

Likes

76 likes

Total Posts

522 posts

Correct Reply

259 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 25
Boost 10
View profile
justin_at_adobe
Employee

15-10-2015

You are missing the Sling-Models-Packages manifest header.

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct Reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct Reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

15-10-2015

Here an example how to add this..

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Sling-Model-Packages> your.model.package </Sling-Model-Packages> </instructions> </configuration> </plugin>