使用data-sly-use.Object会导致隐藏组件

发布于 2025-02-03 23:10:00 字数 3069 浏览 2 评论 0原文

我正在尝试在AEM WKND( https://experienceleaeague.adobe.adobe.com/docs/docs/experience-manager-manager-learn/getting-start---------- -aractype/custom-component.html?lang = en#byline风格)。但是,当我尝试实现HTML文件时,每当我调用数据塞使用API​​时,Byline组件都隐藏在页面中。这是否有任何解决方法或解决方案?

以下是我的IMPS代码:

@Model(adaptables = {SlingHttpServletRequest.class},
       adapters = {Byline.class},
       resourceType = {BylineImpl.RESOURCE_TYPE},
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class BylineImpl implements Byline {
protected static final String RESOURCE_TYPE = "wknd/components/byline";

    @Self
    private SlingHttpServletRequest request;

    @OSGiService
    private ModelFactory modelFactory;

    @ValueMapValue
    private String name;

    @ValueMapValue
    private List<String> occupations;

    private Image image;

    @Override
    public String getName() {
        return name;
    }

    @PostConstruct
    private void init(){
        image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);

    }

    @Override
    public List<String> getOccupations() {
        if (occupations != null) {
            Collections.sort(occupations);
            return new ArrayList<String>(occupations);
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public boolean isEmpty() {
        final Image componentImage = getImage();

        if (StringUtils.isBlank(name)) {
            // Name is missing, but required
            return true;
        } else if (occupations == null || occupations.isEmpty()) {
            // At least one occupation is required
            return true;
        } else if (componentImage == null || StringUtils.isBlank(componentImage.getSrc())) {
            // A valid image is required
            return true;
        } else {
            // Everything is populated, so this component is not considered empty
            return false;
        }
    }

    private Image getImage() {
        return image;
    }
}

以下是我的byline.html代码:

div data-sly-use.byline="com.adobe.aem.guides.wknd.core.models.impl.BylineImpl" 
data-sly-use.placeholderTemplate="core/wcm/components/commons/v1/templates.html" 
data-sly-test.hasContent="${!byline.empty}" class="cmp-byline">
      <div class="cmp-byline__image" data-sly-resource="${ '.' @ resourceType = 'core/wcm/components/image/v2/image' }">
      </div>
      <h2 class="cmp-byline__name">${byline.name}</h2>
      <p class="cmp-byline__occupations">${byline.occupations @ join=', '}</p>
 </div>
 <sly data-sly-call="${placeholderTemplate.placeholder @ isEmpty=!hasContent}"></sly>

I am trying to recreate the Byline component activity in AEM WKND (https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/project-archetype/custom-component.html?lang=en#byline-styles). However when I tried to implement the html file, byline component is hidden in the page whenever I call data-sly-use api. Is there any workaround or solution for this one?

Below is my impl code:

@Model(adaptables = {SlingHttpServletRequest.class},
       adapters = {Byline.class},
       resourceType = {BylineImpl.RESOURCE_TYPE},
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class BylineImpl implements Byline {
protected static final String RESOURCE_TYPE = "wknd/components/byline";

    @Self
    private SlingHttpServletRequest request;

    @OSGiService
    private ModelFactory modelFactory;

    @ValueMapValue
    private String name;

    @ValueMapValue
    private List<String> occupations;

    private Image image;

    @Override
    public String getName() {
        return name;
    }

    @PostConstruct
    private void init(){
        image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);

    }

    @Override
    public List<String> getOccupations() {
        if (occupations != null) {
            Collections.sort(occupations);
            return new ArrayList<String>(occupations);
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public boolean isEmpty() {
        final Image componentImage = getImage();

        if (StringUtils.isBlank(name)) {
            // Name is missing, but required
            return true;
        } else if (occupations == null || occupations.isEmpty()) {
            // At least one occupation is required
            return true;
        } else if (componentImage == null || StringUtils.isBlank(componentImage.getSrc())) {
            // A valid image is required
            return true;
        } else {
            // Everything is populated, so this component is not considered empty
            return false;
        }
    }

    private Image getImage() {
        return image;
    }
}

Below is my byline.html code:

div data-sly-use.byline="com.adobe.aem.guides.wknd.core.models.impl.BylineImpl" 
data-sly-use.placeholderTemplate="core/wcm/components/commons/v1/templates.html" 
data-sly-test.hasContent="${!byline.empty}" class="cmp-byline">
      <div class="cmp-byline__image" data-sly-resource="${ '.' @ resourceType = 'core/wcm/components/image/v2/image' }">
      </div>
      <h2 class="cmp-byline__name">${byline.name}</h2>
      <p class="cmp-byline__occupations">${byline.occupations @ join=', '}</p>
 </div>
 <sly data-sly-call="${placeholderTemplate.placeholder @ isEmpty=!hasContent}"></sly>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-02-10 23:10:00

尝试修改data-sly-use.byline =“ com.adobe.aem.guides.wknd.core.models.impl.impl.impl.bylineimpl” to data-sly-use.byline =” com.adobe.aem.guides.wknd.core.models.byline“

Try to modify data-sly-use.byline="com.adobe.aem.guides.wknd.core.models.impl.BylineImpl" to data-sly-use.byline="com.adobe.aem.guides.wknd.core.models.Byline"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文