Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 7

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

 

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
    @getter
    private int count;

    @ValueMapValue
    @getter
    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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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