代码之家  ›  专栏  ›  技术社区  ›  Vivek Dhiman

无法将模型类调整为slinghttpservletrequest

  •  0
  • Vivek Dhiman  · 技术社区  · 6 年前

    我正试图在我的模型类中注入资源。使用注释时遇到的问题 @Model(adaptables = { SlingHttpServletRequest.class, Resource.class })

    我将对象作为空值获取,而仅使用resource.class,我将获取对象(navigationitems)。下面是我班的一小段。你能告诉我修理它的步骤吗?

    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.models.annotations.Model;
    import org.apache.sling.models.annotations.Optional;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import javax.annotation.PostConstruct;
    import javax.inject.Inject;
    import javax.inject.Named;
    import java.util.List;
    
    @Model(adaptables =  { SlingHttpServletRequest.class, Resource.class })
    public class Header {
        private final Logger logger = LoggerFactory.getLogger(getClass());
    
        @Inject
        @Optional
        @Named("navitems")
        private Resource navigationItems;
    
        List<SiteNavigation> siteNavigationList;
    
        @PostConstruct
        protected void init() {
            logger.info("In init method of header model.");
            siteNavigationList = getSiteNavigationListItems(getNavigationItems());
        }
    
        private List<SiteNavigation> getSiteNavigationListItems(final Resource navigationItems, final Resource columnFourItems) {
            return null;
        }
    
        public Resource getNavigationItems() {
            return navigationItems;
        }
    }
    

    如果删除可选注释,则会出现以下错误:

    28.09.2018 14:04:39.735 *ERROR* [0:0:0:0:0:0:0:1 [1538123679033] GET /conf/myprj/settings/wcm/templates/homepage/structure.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/conf/myprj/settings/wcm/templates/homepage/structure/jcr:content/root/header', selectorString='null', extension='html', suffix='null'
    org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.myprj.core.models.Header cannot be correctly instantiated by the Use API
        at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.54]
        at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.6.12]
    
        Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.myprj.core.models.Header
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:679) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
        at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:394) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
        at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:261) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
        at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:126) [org.apache.sling.scripting.sightly.models.provider:1.0.6]
        at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]
        ... 243 common frames omitted
        Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private org.apache.sling.api.resource.Resource com.myprj.core.models.Header.navigationItems
            at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:684) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
            ... 247 common frames omitted
        Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
            at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:581) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
            at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:682) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
            ... 247 common frames omitted
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Saravana Prakash    6 年前

    代码中有3个指针:

    1. 虽然sling9支持多个可适配项,但最好 adapt from slinghttpservetlRequest对象。它位于较高的层,并包裹着大多数其他对象。
    2. 建议您增加喷油器 specific 而不是一般的@inject。
    3. 总是指定 resourceType attribute 将模型与特定资源类型关联。对于碎片的导出者来说,吊索可以关联得更紧密,可读性更好。

    这将是我对你的吊索型号的代码:

    @Model(adaptables = SlingHttpServletRequest.class, resourceType = "myprj/components/content/header",
      defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
    public class Header {
    
      @ChildResource
      private Resource navitems; // Keeping resource name and attribute name identical reduces @Named annotation
    
        2
  •  0
  •   Vivek Dhiman    6 年前

    此问题通过@via注释解决。下面是我使用的代码片段。

    @Inject
        @Via("resource")
        @Named("navitems")
        private Resource navigationItems;
    
    推荐文章