While Creating SPA Component Model cant get data into json of other method apart of Injected | Community
Skip to main content
New Participant
November 14, 2022
Solved

While Creating SPA Component Model cant get data into json of other method apart of Injected

  • November 14, 2022
  • 1 reply
  • 464 views

In my en.model.json I can't fetch data from the method which doesn't have any inject or @9944223

 

For the below sample code In en.model.json queryedData is not coming up

@Model(adaptables = SlingHttpServletRequest.class, adapters = { SampleModel.class, ComponentExporter.class }, resourceType = SampleModel.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) @Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION) public class SampleModel { private static final Logger log = LoggerFactory.getLogger(SampleModel.class); static final String RESOURCE_TYPE = "aem-example/components/sample"; @ValueMapValue @14766979 private int count; @ValueMapValue @14766979 private String path; public List<String> queryedData(){ List<String> abc = new ArrayList<>(); abc.add("Sample"); abc.add("Return"); return abc; } }


In en.model.json queryedData is not coming up

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 arunpatidar

I have tried this before and it worked with me. I can share the sample code

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.community.aemlab.spa.core.models.Cards;
import com.community.aemlab.spa.core.models.LinkModel;

@Model(adaptables = SlingHttpServletRequest.class, adapters = { Cards.class,
		ComponentExporter.class }, resourceType = CardsImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class CardsImpl implements Cards {

	static final String RESOURCE_TYPE = "aemlab-spa/components/custom/cards";

	@Self
	private SlingHttpServletRequest request;

	@ChildResource(name = "cta")
	private LinkModel cta;

	@ValueMapValue
	private String cardLayout;

	@ValueMapValue
	private String title;

	@ValueMapValue
	private String description;

	@Override
	public LinkModel getCardLink() {
		return cta;
	}

	@Override
	public String getCardDescription() {
		return description;
	}

	@Override
	public String getCardTitle() {
		return title;
	}

	@Override
	public String getCardLayout() {
		return cardLayout;
	}

	@Override
	public String getExportedType() {
		return CardsImpl.RESOURCE_TYPE;
	}

}

Interface

package com.community.aemlab.spa.core.models;

import org.osgi.annotation.versioning.ProviderType;
import com.adobe.cq.export.json.ComponentExporter;

@ProviderType
public interface Cards extends ComponentExporter {
	public LinkModel getCardLink();
        public String getCardDescription();
	public String getCardTitle();
	public String getCardLayout();
}

1 reply

arunpatidar
arunpatidarAccepted solution
New Participant
November 14, 2022

I have tried this before and it worked with me. I can share the sample code

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.community.aemlab.spa.core.models.Cards;
import com.community.aemlab.spa.core.models.LinkModel;

@Model(adaptables = SlingHttpServletRequest.class, adapters = { Cards.class,
		ComponentExporter.class }, resourceType = CardsImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class CardsImpl implements Cards {

	static final String RESOURCE_TYPE = "aemlab-spa/components/custom/cards";

	@Self
	private SlingHttpServletRequest request;

	@ChildResource(name = "cta")
	private LinkModel cta;

	@ValueMapValue
	private String cardLayout;

	@ValueMapValue
	private String title;

	@ValueMapValue
	private String description;

	@Override
	public LinkModel getCardLink() {
		return cta;
	}

	@Override
	public String getCardDescription() {
		return description;
	}

	@Override
	public String getCardTitle() {
		return title;
	}

	@Override
	public String getCardLayout() {
		return cardLayout;
	}

	@Override
	public String getExportedType() {
		return CardsImpl.RESOURCE_TYPE;
	}

}

Interface

package com.community.aemlab.spa.core.models;

import org.osgi.annotation.versioning.ProviderType;
import com.adobe.cq.export.json.ComponentExporter;

@ProviderType
public interface Cards extends ComponentExporter {
	public LinkModel getCardLink();
        public String getCardDescription();
	public String getCardTitle();
	public String getCardLayout();
}
Arun Patidar