Issues with retrieving Sling Model values in JSP
Hello Community - I am getting an error while retrieving the values from Sling Model and using it in JSP. Not sure what is the issue? Can someone help with this?
Error:
org.apache.sling.models.impl.ModelAdapterFactory Could not adapt to model
org.apache.sling.models.factory.ModelClassException: Unable to find a useable constructor for model class com.test.aem.core.models.TestModel
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:686) [org.apache.sling.models.impl:1.4.12]
Sling Model:
package com.test.aem.core.models;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
/**
* Model used to set & get values
*/
@Model(adaptables = {SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestModel {
private SlingHttpServletRequest request;
public TestModel(SlingHttpServletRequest request) {
this.request = request;
}
private String articleInfo;
private String articleFound = "false";
public String getArticleInfo() {
return articleInfo;
}
public void setArticleInfo(String articleInfo) {
this.articleInfo = articleInfo;
}
public String getArticleFound() {
return articleFound;
}
public void setArticleFound(String articleFound) {
this.articleFound = articleFound;
}
@PostConstruct
protected void init() {
if (request.getQueryString() != null) {
this.setArticleFound("true");
}
String jcrPath = "/content/test/jcr:content/Par/config";
Node nodeVal = request.getResourceResolver().getResource(jcrPath).adaptTo(Node.class);
try {
articleInfo = nodeVal != null ? nodeVal.getProperty("articleInfo").getString() : "";
this.setArticleInfo(articleInfo);
} catch (RepositoryException e) {
}
}
}
JSP:
<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" import="com.test.aem.core.models.TestModel"%>
<c:set var="SlingArticleModel" value="<%= resource.adaptTo(TestModel.class)%>"/>
<div>Article Info : ${SlingArticleModel.articleInfo}</div>
@briankasingli, @Vijayalakshmi_S
