I have put my model class in the bundle class.
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
Views
Replies
Total Likes
One thing that jumps out immediately is that your adaptTo class name is myDataModel (note the lower case 'm'), whereas you actual class name is MyDataModel (upper case 'M').
HTHs
Fraser
Views
Replies
Total Likes
goffinf wrote...
One thing that jumps out immediately is that your adaptTo class name is myDataModel (note the lower case 'm'), whereas you actual class name is MyDataModel (upper case 'M').
HTHs
Fraser
Thanks, thats one mistake I made. I have updated the class name in JSP page,but still it is not showing up.
Views
Replies
Total Likes
So in your JSP, what is ${resource} ?
Fraser.
Views
Replies
Total Likes
goffinf wrote...
So in your JSP, what is ${resource} ?
Fraser.
I am not sure about this. Isn't it the implicit object in cq5?
Views
Replies
Total Likes
Hmmm I thought if you want to access the Sling resource associated with the request URL you would need a sling:defineObjects somewhere in your JSP ?
That aside, I assume you did actually install the Sling Models OSGI bundles ?
Fraser.
Views
Replies
Total Likes
goffinf wrote...
Hmmm I thought if you want to access the Sling resource associated with the request URL you would need a sling:defineObjects somewhere in your JSP ?
That aside, I assume you did actually install the Sling Models OSGI bundles ?
Fraser.
I added the <sling:defineObjects />but no luck.
I put this model java class within the component folder if this is what you mean by sling model osgi bundle..
Views
Replies
Total Likes
Your model class needs to be in an OSGi bundle with the appropriate bundle header. It cannot be just a bare Java file in the repository.
Views
Replies
Total Likes
justin_at_adobe wrote...
Your model class needs to be in an OSGi bundle with the appropriate bundle header. It cannot be just a bare Java file in the repository.
I have put my model class in the bundle class.
package com.techsharp.mytestweb.model; 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; } }
And in my bundle pom.xml file, i added these two dependencies
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
Now, I get this... Any idea?
[img]2014-06-24_11-08-50.png[/img]
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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]
Views
Replies
Total Likes
You are missing the Sling-Models-Packages manifest header.
Views
Replies
Total Likes
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>
Views
Replies
Total Likes
Rui J wrote...
goffinf wrote...
Hmmm I thought if you want to access the Sling resource associated with the request URL you would need a sling:defineObjects somewhere in your JSP ?
That aside, I assume you did actually install the Sling Models OSGI bundles ?
Fraser.
I added the <sling:defineObjects />but no luck.
I put this model java class within the component folder if this is what you mean by sling model osgi bundle..
I meant installing the package containing the Sling Models support bundles which you can get here :-
Fraser.
Views
Replies
Total Likes