【记录四】AEM自定义组件

涉及相关

【记录四】AEM自定义组件_第1张图片

目的

了解如何构建完全自定义的组件。

学习使用Sling模型封装业务逻辑。

了解如何在HTL脚本中使用Sling模型。

效果

【记录四】AEM自定义组件_第2张图片

开始创造

这个新组件的实现包括一个对话框,该对话框收集副标题内容,并且自定义Sling模型动态检索副标题的
名称
图片
职业

创建Byline组件

首先,创建Byline组件节点结构并定义一个对话框。这表示AEM中的组件,并通过其在JCR中的位置隐式定义了组件的资源类型。
该对话框显示内容作者可以提供的界面。对于此实现,将利用AEM WCM核心组件的 Image 组件来处理Byline图像的创作和渲染,因此将其设置为组件的 sling:resourceSuperType

创建组件节点
1、在 UI .apps 模块下方 /apps/wknd/components/content 创建名为byline 类型的 cq:Component 的新节点 。
【记录四】AEM自定义组件_第3张图片
.content.xml 节点属性文件内容如下:


<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:description="Displays a contributor's byline."
    jcr:primaryType="cq:Component"
    jcr:title="Byline"
    sling:resourceSuperType="core/wcm/components/image/v2/image"
    componentGroup="WKND.Content"/>

创建HTL脚本

1、下面的 byline 节点,添加的文件名为 byline.html 负责组件的HTML呈现。将文件命名为与 cq:Component节点相同很重要,因为这使它成为Sling用来呈现此资源类型的默认脚本。
【记录四】AEM自定义组件_第4张图片

2、将以下代码添加到 byline.html中

 
 <div data-sly-use.placeholderTemplate="core/wcm/components/commons/v1/templates.html">
 div>
 <sly data-sly-call="${placeholderTemplate.placeholder @ isEmpty=true}">sly>

一旦创建了Sling模型,便会再次访问byline.html 。HTL文件的当前状态允许组件在拖放到页面上时在AEM站点的页面编辑器中显示。

创建对话框定义

【记录四】AEM自定义组件_第5张图片
接下来,使用以下字段为Byline组件定义一个对话框:
Name :名字
Image :头像
Occupations :职业列表。应按字母升序(a到z)对职业进行排序。

1、在 byline 组件节点下,创建一个名为cq:dialog 的新节点 ,类型为 nt:unstructured ;
2、使用以下XML 更新 cq:dialog 。打开.content.xml 并将以下XML复制/粘贴到其中


<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
  jcr:primaryType="nt:unstructured"
  jcr:title="Byline"
  sling:resourceType="cq/gui/components/authoring/dialog">
  <content
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/container">
    <items jcr:primaryType="nt:unstructured">
      <tabs
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/coral/foundation/tabs"
        maximized="{Boolean}false">
        <items jcr:primaryType="nt:unstructured">
          
          <asset
            jcr:primaryType="nt:unstructured"
            sling:hideResource="{Boolean}false"/>
          
          <metadata
            jcr:primaryType="nt:unstructured"
            sling:hideResource="{Boolean}true"/>
          <properties
            jcr:primaryType="nt:unstructured"
            jcr:title="Properties"
            sling:resourceType="granite/ui/components/coral/foundation/container"
            margin="{Boolean}true">
            <items jcr:primaryType="nt:unstructured">
              <columns
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
                margin="{Boolean}true">
                <items jcr:primaryType="nt:unstructured">
                  <column
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="granite/ui/components/coral/foundation/container">
                    <items jcr:primaryType="nt:unstructured">
                      <name
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                        fieldLabel="Name"
                        fieldDescription="The contributor's name to display."
                        emptyText="Enter the contributor's name to display."             name="./name"
                        required="{Boolean}true"/>
                      <occupations
                        jcr:primaryType="nt:unstructured"
                        fieldLabel="Occupations"
                        fieldDescription="A list of the contributor's occupations."
                        required="{Boolean}false"
                        sling:resourceType="granite/ui/components/coral/foundation/form/multifield">
                        <field
                          jcr:primaryType="nt:unstructured"
                          name="./occupations"
                          emptyText="Enter an occupation"
                          sling:resourceType="granite/ui/components/coral/foundation/form/textfield"/>
                      occupations>
                    items>
                  column>
                items>
              columns>
            items>
          properties>
        items>
      tabs>
    items>
  content>
jcr:root>

创建策略对话框

遵循与创建对话框相同的方法,创建“策略”对话框(以前称为“设计对话框”),以隐藏从核心组件的图像组件继承的策略配置中不需要的字段。
1、在 byline cq:Component 节点下,创建一个名为cq:design_dialog的新节点 ,类型为nt:unstructured
2、使用以下XML 更新 cq:design_dialog 。打开.content.xml 并将下面的XML复制/粘贴到其中是最容易的


<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
  xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
  xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
  jcr:primaryType="nt:unstructured"
  jcr:title="Byline"
  sling:resourceType="cq/gui/components/authoring/dialog">
  <content
    jcr:primaryType="nt:unstructured">
    <items jcr:primaryType="nt:unstructured">
      <tabs
        jcr:primaryType="nt:unstructured">
        <items jcr:primaryType="nt:unstructured">
          <properties
            jcr:primaryType="nt:unstructured">
            <items jcr:primaryType="nt:unstructured">
              <content
                jcr:primaryType="nt:unstructured">
                <items jcr:primaryType="nt:unstructured">
                  <decorative
                    jcr:primaryType="nt:unstructured"
                    sling:hideResource="{Boolean}true"/>
                  <altValueFromDAM
                    jcr:primaryType="nt:unstructured"
                    sling:hideResource="{Boolean}true"/>
                  <titleValueFromDAM
                    jcr:primaryType="nt:unstructured"
                    sling:hideResource="{Boolean}true"/>
                  <displayCaptionPopup
                    jcr:primaryType="nt:unstructured"
                    sling:hideResource="{Boolean}true"/>
                  <disableUuidTracking
                    jcr:primaryType="nt:unstructured"
                    sling:hideResource="{Boolean}true"/>
                items>
              content>
            items>
          properties>
          <features
            jcr:primaryType="nt:unstructured">
            <items jcr:primaryType="nt:unstructured">
              <content
                jcr:primaryType="nt:unstructured">
                <items jcr:primaryType="nt:unstructured">
                  <accordion
                    jcr:primaryType="nt:unstructured">
                    <items jcr:primaryType="nt:unstructured">
                      <orientation
                        jcr:primaryType="nt:unstructured"
                        sling:hideResource="{Boolean}true"/>
                      <crop
                        jcr:primaryType="nt:unstructured"
                        sling:hideResource="{Boolean}true"/>
                    items>
                  accordion>
                items>
              content>
            items>
          features>
        items>
      tabs>
    items>
  content>
jcr:root>

创建Byline Sling模型

Sling模型以充当数据模型并容纳Byline组件的业务逻辑。
Sling模型是注释驱动的Java“ POJO”(普通的旧Java对象),它有助于将数据从JCR映射到Java变量

更新pom.xml 文件

1、打开父POM: aem -guides-wknd / pom.xml
2、确保 uber-jar 依赖性为 6.3.2 或更高。

            
            <dependency>
                <groupId>com.adobe.aemgroupId>
                <artifactId>uber-jarartifactId>
                <version>6.4.5version>
                <classifier>apisclassifier>
                <scope>providedscope>
            dependency>

3、在父POM中,为Core Components 添加依赖项 。这是与核心组件关联的Sling模型的依赖项。core.wcm.components.all 、core.wcm.components.core
【记录四】AEM自定义组件_第6张图片
4、完整的父 POM.XML



<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.adobe.aem.guidesgroupId>
    <artifactId>aem-guides-wkndartifactId>
    <packaging>pompackaging>
    <version>0.0.1-SNAPSHOTversion>
    <description>WKND Sites Projectdescription>

    <modules>
        <module>coremodule>
        <module>ui.appsmodule>
        <module>ui.contentmodule>
        <module>it.testsmodule>
        <module>it.launchermodule>
    modules>

    <properties>
        <aem.host>localhostaem.host>
        <aem.port>4502aem.port>
        <aem.publish.host>localhostaem.publish.host>
        <aem.publish.port>4503aem.publish.port>
        <sling.user>adminsling.user>
        <sling.password>adminsling.password>
        <vault.user>adminvault.user>
        <vault.password>adminvault.password>

        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    properties>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-release-pluginartifactId>
                <version>2.5.3version>
                <configuration>
                    <scmCommentPrefix>[maven-scm] :scmCommentPrefix>
                    <preparationGoals>clean installpreparationGoals>
                    <goals>installgoals>
                    <releaseProfiles>releasereleaseProfiles>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <version>3.0.1version>
                <inherited>trueinherited>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <version>3.0.2version>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-enforcer-pluginartifactId>
                <executions>
                    <execution>
                        <id>enforce-mavenid>
                        <goals>
                            <goal>enforcegoal>
                        goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <version>[3.3.9,)version>
                                requireMavenVersion>
                                <requireJavaVersion>
                                    <message>Project must be compiled
                                        with Java 8 or highermessage>
                                    <version>1.8.0version>
                                requireJavaVersion>
                            rules>
                        configuration>
                    execution>
                executions>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-idea-pluginartifactId>
                <version>2.2.1version>
                <configuration>
                    <jdkLevel>1.8jdkLevel>
                    <linkModules>truelinkModules>
                    <downloadSources>truedownloadSources>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-eclipse-pluginartifactId>
                <version>2.10version>
                <configuration>
                    <downloadSources>truedownloadSources>
                configuration>
            plugin>
        plugins>
        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.0.0version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.6.1version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                    <configuration>
                        <useSystemClassLoader>falseuseSystemClassLoader>
                    configuration>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-failsafe-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.slinggroupId>
                    <artifactId>maven-sling-pluginartifactId>
                    <version>2.2.0version>
                    <configuration>
                        <slingUrl>http://${aem.host}:${aem.port}/system/consoleslingUrl>
                        <deploymentMethod>WebConsoledeploymentMethod>
                    configuration>
                plugin>
                
                <plugin>
                    <groupId>org.apache.slinggroupId>
                    <artifactId>htl-maven-pluginartifactId>
                    <version>1.0.6version>
                    <configuration>
                        <failOnWarnings>truefailOnWarnings>
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>validategoal>
                            goals>
                        execution>
                    executions>
                plugin>
                
                <plugin>
                    <groupId>org.apache.jackrabbitgroupId>
                    <artifactId>filevault-package-maven-pluginartifactId>
                    <version>1.0.3version>
                    <configuration>
                        <filterSource>src/main/content/META-INF/vault/filter.xmlfilterSource>
                    configuration>
                plugin>
                
                <plugin>
                    <groupId>com.day.jcr.vaultgroupId>
                    <artifactId>content-package-maven-pluginartifactId>
                    <version>1.0.2version>
                    <configuration>
                        <targetURL>http://${aem.host}:${aem.port}/crx/packmgr/service.jsptargetURL>
                        <failOnError>truefailOnError>
                        <userId>${vault.user}userId>
                        <password>${vault.password}password>
                    configuration>
                plugin>
                
                <plugin>
                    <groupId>org.apache.felixgroupId>
                    <artifactId>maven-bundle-pluginartifactId>
                    <version>4.2.1version>
                    <inherited>trueinherited>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-enforcer-pluginartifactId>
                    <version>1.4.1version>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-dependency-pluginartifactId>
                    <version>3.0.0version>
                plugin>
                
                <plugin>
                    <groupId>org.codehaus.mojogroupId>
                    <artifactId>build-helper-maven-pluginartifactId>
                    <version>3.0.0version>
                plugin>
                
                <plugin>
                    <groupId>org.eclipse.m2egroupId>
                    <artifactId>lifecycle-mappingartifactId>
                    <version>1.0.0version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.pluginsgroupId>
                                        <artifactId>maven-enforcer-pluginartifactId>
                                        <versionRange>[1.0.0,)versionRange>
                                        <goals>
                                            <goal>enforcegoal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.maven.plugins
                                        groupId>
                                        <artifactId>
                                            maven-dependency-plugin
                                        artifactId>
                                        <versionRange>
                                            [2.2,)
                                        versionRange>
                                        <goals>
                                            <goal>copy-dependenciesgoal>
                                            <goal>unpackgoal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.codehaus.mojo
                                        groupId>
                                        <artifactId>
                                            build-helper-maven-plugin
                                        artifactId>
                                        <versionRange>
                                            [1.5,)
                                        versionRange>
                                        <goals>
                                            <goal>
                                                reserve-network-port
                                            goal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                            pluginExecutions>
                        lifecycleMappingMetadata>
                    configuration>
                plugin>
            plugins>
        pluginManagement>
    build>

    <profiles>
        
        
        
        <profile>
            <id>adobe-publicid>

            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>

            <properties>
                <releaseRepository-Id>adobe-public-releasesreleaseRepository-Id>
                <releaseRepository-Name>Adobe Public ReleasesreleaseRepository-Name>
                <releaseRepository-URL>https://repo.adobe.com/nexus/content/groups/publicreleaseRepository-URL>
            properties>

            <repositories>
                <repository>
                    <id>adobe-public-releasesid>
                    <name>Adobe Public Repositoryname>
                    <url>https://repo.adobe.com/nexus/content/groups/publicurl>
                    <releases>
                        <enabled>trueenabled>
                        <updatePolicy>neverupdatePolicy>
                    releases>
                    <snapshots>
                        <enabled>falseenabled>
                    snapshots>
                repository>
            repositories>

            <pluginRepositories>
                <pluginRepository>
                    <id>adobe-public-releasesid>
                    <name>Adobe Public Repositoryname>
                    <url>https://repo.adobe.com/nexus/content/groups/publicurl>
                    <releases>
                        <enabled>trueenabled>
                        <updatePolicy>neverupdatePolicy>
                    releases>
                    <snapshots>
                        <enabled>falseenabled>
                    snapshots>
                pluginRepository>
            pluginRepositories>
        profile>

        
        <profile>
            <id>autoInstallBundleid>
            
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.slinggroupId>
                            <artifactId>maven-sling-pluginartifactId>
                            <executions>
                                <execution>
                                    <id>install-bundleid>
                                    <goals>
                                        <goal>installgoal>
                                    goals>
                                execution>
                            executions>
                        plugin>
                    plugins>
                pluginManagement>
            build>
        profile>

        <profile>
            <id>autoInstallPackageid>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.jackrabbitgroupId>
                            <artifactId>filevault-package-maven-pluginartifactId>
                            <executions>
                                <execution>
                                    <id>create-packageid>
                                    <goals>
                                        <goal>packagegoal>
                                    goals>
                                execution>
                            executions>
                        plugin>
                        <plugin>
                            <groupId>com.day.jcr.vaultgroupId>
                            <artifactId>content-package-maven-pluginartifactId>
                            <executions>
                                <execution>
                                    <id>install-packageid>
                                    <goals>
                                        <goal>installgoal>
                                    goals>
                                    <configuration>
                                        <targetURL>http://${aem.host}:${aem.port}/crx/packmgr/service.jsptargetURL>
                                    configuration>
                                execution>
                            executions>
                        plugin>
                    plugins>
                pluginManagement>
            build>
        profile>

        <profile>
            <id>autoInstallPackagePublishid>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.jackrabbitgroupId>
                            <artifactId>filevault-package-maven-pluginartifactId>
                            <executions>
                                <execution>
                                    <id>create-packageid>
                                    <goals>
                                        <goal>packagegoal>
                                    goals>
                                execution>
                            executions>
                        plugin>
                        <plugin>
                            <groupId>com.day.jcr.vaultgroupId>
                            <artifactId>content-package-maven-pluginartifactId>
                            <executions>
                                <execution>
                                    <id>install-package-publishid>
                                    <goals>
                                        <goal>installgoal>
                                    goals>
                                    <configuration>
                                        <targetURL>http://${aem.publish.host}:${aem.publish.port}/crx/packmgr/service.jsptargetURL>
                                    configuration>
                                execution>
                            executions>
                        plugin>
                    plugins>
                pluginManagement>
            build>
        profile>

    profiles>


    
    
    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.osgigroupId>
                <artifactId>osgi.coreartifactId>
                <version>6.0.0version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.osgigroupId>
                <artifactId>osgi.cmpnartifactId>
                <version>6.0.0version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.osgigroupId>
                <artifactId>osgi.annotationartifactId>
                <version>6.0.1version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.injectgroupId>
                <artifactId>javax.injectartifactId>
                <version>1version>
            dependency>
            <dependency>
                <groupId>javax.annotationgroupId>
                <artifactId>javax.annotation-apiartifactId>
                <version>1.3.2version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.7.21version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>com.adobe.aemgroupId>
                <artifactId>uber-jarartifactId>
                <version>6.4.5version>
                <classifier>apisclassifier>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>com.adobe.cqgroupId>
                <artifactId>core.wcm.components.allartifactId>
                <type>ziptype>
                <version>2.3.2version>
            dependency>
            <dependency>
                <groupId>com.adobe.cqgroupId>
                <artifactId>core.wcm.components.examplesartifactId>
                <type>ziptype>
                <version>2.3.2version>
            dependency>
            <dependency>
                <groupId>com.adobe.cqgroupId>
                <artifactId>core.wcm.components.coreartifactId>
                <version>2.2.0version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>org.apache.slinggroupId>
                <artifactId>org.apache.sling.models.apiartifactId>
                <version>1.3.6version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.servlet.jspgroupId>
                <artifactId>jsp-apiartifactId>
                <version>2.1version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>javax.jcrgroupId>
                <artifactId>jcrartifactId>
                <version>2.0version>
                <scope>providedscope>
            dependency>
            
            <dependency>
                <groupId>com.day.cq.wcmgroupId>
                <artifactId>cq-wcm-taglibartifactId>
                <version>5.7.4version>
                <scope>providedscope>
            dependency>

            
            <dependency>
                <groupId>org.junitgroupId>
                <artifactId>junit-bomartifactId>
                <version>5.4.1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-simpleartifactId>
                <version>1.7.25version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.mockitogroupId>
                <artifactId>mockito-coreartifactId>
                <version>2.25.1version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.mockitogroupId>
                <artifactId>mockito-junit-jupiterartifactId>
                <version>2.25.1version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>junit-addonsgroupId>
                <artifactId>junit-addonsartifactId>
                <version>1.4version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>io.wcmgroupId>
                <artifactId>io.wcm.testing.aem-mock.junit5artifactId>
                <version>2.4.4version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>uk.org.lidaliagroupId>
                <artifactId>slf4j-testartifactId>
                <version>1.0.1version>
                <scope>testscope>
            dependency>
        dependencies>

    dependencyManagement>

project>

Byline组件对应的接口

1、为Byline创建一个公共Java接口,Byline.java 定义驱动byline.html HTL脚本所需的公共方法
2、在src / main / java下面的 aem-guides-wknd.core 模块中创建一个名为com.adobe.aem.guides.wknd.core.components 的新Java包
3、com.adobe.aem.guides.wknd.core.components 创建一个名为Byline.java 的新Java 接口

package com.adobe.aem.guides.wknd.core.components;

import java.util.List;

/**
 * 代表WKND Site项目的Byline AEM组件
 *
 * @author kevin
 * @title: Byline
 * @projectName aem-guides-wknd
 * @date 2019/10/1614:35
 */
public interface Byline {

  /***
   * @return 返回字符串以显示为名称.
   */
  String getName();

  /***
   * 职业将按字母降序排列.
   *
   * @return 返回职业列表.
   */
  List<String> getOccupations();

  /***
   * @return 返回布尔值(如果组件具有要显示的内容).
   */
  boolean isEmpty();
}

创建Byline.java 的实现类BylineImpl.java

1、com.adobe.aem.guides.wknd.core.components.impl包下创建BylineImpl.java
2、通过使用以下类级别的注释更新BylineImpl.java来 添加Sling Model 注释。此 @Model(…)批注将把 类变成Sling模型

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
...
@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/content/byline";
    ...
}

说明:
@Model : 声明为Sling模型
adaptables = {SlingHttpServletRequest.class}: 作用是:将sling模型映射到sling资源上(此模型可以由请求)
resourceType = {BylineImpl.RESOURCE_TYPE} : 指向署名组件的资源类型
defaultInjectionStrategy : 指示sling模型中的注入字段是否应该是必需的/可选的

3、完整代码

package com.adobe.aem.guides.wknd.core.components.impl;

import com.adobe.aem.guides.wknd.core.components.Byline;
import com.adobe.cq.wcm.core.components.models.Image;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.factory.ModelFactory;

/**
 * todo
 *
 * @author kevin
 * @title: BylineImpl
 * @projectName aem-guides-wknd
 * @date 2019/10/1614:39
 */
@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/content/byline";

  @ValueMapValue //注入属性
  private String name;

  @ValueMapValue //注入属性
  //@Named("fullName") 指定名称注入
  private List<String> occupations;
  @OSGiService //注入模型工厂
  private ModelFactory modelFactory;

  @Self //向字段注入可适应的对象获取请求上下文
  private SlingHttpServletRequest request;
  
  //@Self 直接注入image.class资源
  private Image image;

  @PostConstruct //实例化
  private void init() {
    image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);
  }

  /***
   * @return a string to display as the name.
   */
  @Override
  public String getName() {
    return name;
  }

  /***
   * Occupations are to be sorted alphabetically in a descending order.
   *
   * @return a list of occupations.
   */
  @Override
  public List<String> getOccupations() {
    if (occupations != null) {
      Collections.sort(occupations);
      return occupations;
    } else {
      return Collections.emptyList();
    }
  }

  /***
   * @return a boolean if the component has content to display.
   */
  @Override
  public boolean isEmpty() {
    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 (getImage() == null || StringUtils.isBlank(getImage().getSrc())) {
      // A valid image is required
      return true;
    } else {
      // Everything is populated, so this component is not considered empty
      return false;
    }
  }

  private Image getImage() {
    Image image = null;
    // Figure out how to populate the image variable!
    return image;
  }


}

修改 byline.html 完整代码如下

data-sly-use.byline ="com.adobe.aem.guides.wknd.core.components.Byline
引入 Byline Sling模型,后面就可以直接调用公共方法了

将代码库部署到本地AEM实例

【记录四】AEM自定义组件_第7张图片

部署成功status是active

【记录四】AEM自定义组件_第8张图片

如果状态不是active点击名称展开,检查是否有标红错误

1、如果有错误,根据错误进行解决
2、错误原因大概分为,jar包缺失,jar包冲突,jar版本错误
在这里插入图片描述
【记录四】AEM自定义组件_第9张图片

【记录四】AEM自定义组件_第10张图片

到此组件创建完成可以开始使用了

使用组件

1、在模板创建,表单和碎片创建处均可以使用改组件
【记录四】AEM自定义组件_第11张图片
2、从左侧拖到组件到右边即可使用
【记录四】AEM自定义组件_第12张图片
3、编辑组件数据
【记录四】AEM自定义组件_第13张图片
【记录四】AEM自定义组件_第14张图片
【记录四】AEM自定义组件_第15张图片

你可能感兴趣的:(#,9.2,记录)