-1

AEM sling model --Multifield Links component :

MissingElementsException: Could not inject all required fields

I am trying to create a multifield link(URL) component - EXTERNAL AND INTERNAL LINKS. See getpageURL() for understanding.

See the image below:

exception image

HTL and dailog

@Model(adaptables = Resource.class)
public class Links_Bean {

@Inject
private String pagePath;

@PostConstruct
protected void init() {
    pagePath = getPageURL(pagePath);
}

public static String getPageURL(String pagePath) {
    if (pagePath.isEmpty() || (pagePath.equals(null))) {
        return null;
    } else if (pagePath.startsWith("/content")) {

        return pagePath.concat(".html");
    } else if (pagePath.startsWith("http://") || pagePath.startsWith("https://") || pagePath.startsWith("www")) {
        return pagePath;
    }
    return pagePath;
}

public String getPagePath() {
    return pagePath;
}

public void setPagePath(String pagePath) {
    this.pagePath = pagePath;
}

}

package com.hcl.aem.core.models;


@Model(adaptables = Resource.class)
public class MF_newMethod {
@Inject
@Named("items")
public Resource pagePathMF;

public List<Links_Bean> links = new ArrayList<Links_Bean>();

@PostConstruct
protected void init() {
    if (pagePathMF != null) {
        links = getPageList(links, pagePathMF);
    }
}

public static List<Links_Bean> getPageList(List<Links_Bean> array, Resource resource) {
    if (resource != null) {

        Iterator<Resource> linkResource = resource.listChildren();
        while (linkResource.hasNext()) {
            Links_Bean lb = linkResource.next().adaptTo(Links_Bean.class);

            array.add(lb);

        }

    }
    return array;
}

public List<Links_Bean> getLinks() {
    return links;
}

}

enter code here

Kumar Ramela
  • 11
  • 1
  • 6

3 Answers3

0

Please check weather you can able to get pagePath value, If that is not mandatory use @Optional annotation.

Use below link for reference

[https://sling.apache.org/documentation/bundles/models.html][1]

Thanks, Kiran

kiran
  • 1
  • 2
0
      protected String init() {
    pagePath = getPageURL(pagePath);
    return pagePath;  //<--------------added this
   }

and @optional what kiran said....

Kumar Ramela
  • 11
  • 1
  • 6
0

Your are missing an @Optional here:

...

@Inject
@Named("items")
@Optional                      //<--- THIS
public Resource pagePathMF;

...
Evaristo Carmona
  • 306
  • 1
  • 12