Issues with retrieving Sling Model values in JSP | Community
Skip to main content
Level 6
May 28, 2021
Solved

Issues with retrieving Sling Model values in JSP

  • May 28, 2021
  • 2 replies
  • 4608 views

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

 

 

 

 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi @test1234567,

Looks like you are trying to inject the SlingHttpServletRequest via Constructor(couldn't find inject annotation though, not sure if its a typo while pasting the code here in the post), Remove the constructor and you can use either @Self or @SlingObject for injecting SlingHttpServletRequest.

 

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
May 28, 2021

Hi @test1234567 

 

Your Model class is adapted to SlingHttpServletRequest, so in JSP also you should adapt the class into SlingHttpServletRequest(slingRequest in global.jsp).

I see you have adapted into Resource object which is why it's failing.

 

Please update the below line in JSP and it will resolve the issue:

 

<c:set var="SlingArticleModel" value="<%= slingRequest.adaptTo(TestModel.class)%>"/>

 

Thanks! 

Level 6
May 28, 2021
@asutosh_jena_ - Thanks for your reply. The property and the value is present in the node. I am able to print the "articleInfo" value in the init() menthod as well. The only issue is while setting the value, it is not assigning the value. articleInfo = nodeVal != null ? nodeVal.getProperty("cq:conf").getString() : StringUtils.EMPTY;
Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
May 28, 2021

Hi @test1234567,

Looks like you are trying to inject the SlingHttpServletRequest via Constructor(couldn't find inject annotation though, not sure if its a typo while pasting the code here in the post), Remove the constructor and you can use either @Self or @SlingObject for injecting SlingHttpServletRequest.