在AEM中使用Sling Models

使用Sling Models

示例

现在让我们看一个Sling Models的例子

  • 创建一个新的AEM项目(参考前面博文),然后在您的AEM实例中进行部署。
  • 转到CRXDE,然后在/apps中的项目下使用以下配置创建一个新组件
    在AEM中使用Sling Models_第1张图片
  • 现在,使用以下配置在组件节点下创建dialog节点
    -在AEM中使用Sling Models_第2张图片
    在这里插入图片描述
  • 将代码进行同步到本地
    在这里插入图片描述
  • 现在,使用以下配置在组件节点下创建dialog节点

<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" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
          jcr:primaryType="nt:unstructured"
          jcr:title="User Component"
          sling:resourceType="cq/gui/components/authoring/dialog">
    <content
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/container">
        <layout
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/layouts/tabs"
                type="nav"/>
        <items jcr:primaryType="nt:unstructured">
            <properties
                    jcr:primaryType="nt:unstructured"
                    jcr:title="User Properties"
                    sling:resourceType="granite/ui/components/foundation/container">
                <layout
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns"/>
                <items jcr:primaryType="nt:unstructured">
                    <columns
                            jcr:primaryType="nt:unstructured"
                            sling:resourceType="granite/ui/components/foundation/container">
                        <items jcr:primaryType="nt:unstructured">
                            <firstName
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/textfield"
                                    class="field-whitespace"
                                    fieldDescription="Please enter the first name"
                                    fieldLabel="First Name"
                                    name="./firstName"/>
                            <lastName
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/textfield"
                                    class="field-whitespace"
                                    fieldDescription="Please enter the last name"
                                    fieldLabel="Last Name"
                                    name="./lastName"/>
                            <gender
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/select"
                                    fieldDescription="Select your grnder"
                                    fieldLabel="gender"
                                    name="./gender">
                                <items jcr:primaryType="nt:unstructured">
                                    <male
                                            jcr:primaryType="nt:unstructured"
                                            text="Male"
                                            value="Male"/>
                                    <female
                                            jcr:primaryType="nt:unstructured"
                                            text="Female"
                                            value="Female"/>
                                    <other
                                            jcr:primaryType="nt:unstructured"
                                            text="Other"
                                            value="Other"/>
                                items>
                            gender>
                            <country
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/select"
                                    fieldDescription="Select your country"
                                    fieldLabel="Country"
                                    name="./country">
                                <items jcr:primaryType="nt:unstructured">
                                    <option1
                                            jcr:primaryType="nt:unstructured"
                                            text="India"
                                            value="India"/>
                                    <option2
                                            jcr:primaryType="nt:unstructured"
                                            text="Canada"
                                            value="Canada"/>
                                    <option3
                                            jcr:primaryType="nt:unstructured"
                                            text="Israel"
                                            value="Israel"/>
                                    <option4
                                            jcr:primaryType="nt:unstructured"
                                            text="Other"
                                            value="Other"/>
                                items>
                            country>
                        items>
                    columns>
                items>
            properties>
        items>
    content>
jcr:root>
  • 在您的项目中创建一个新类UserModel.java,并将以下代码粘贴到其中
package com.adobe.aem.guides.wknd.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;

/**
 * @author kevin.
 * @Title: rhtj.
 * @Package com.adobe.aem.guides.wknd.core.models.
 * @Description: sing model.
 * @date 2020/04/3 16:22 下午.
 */
@Model(adaptables = Resource.class)
public class UserModel {

    @Inject
    private String firstName;

    @Inject
    private String lastName;

    @Inject
    private String gender;

    @Inject
    private String country;

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

}
  • 让我们了解该类的代码。我们使用@Model注释指定此模型类为Sling模型。每个数据成员都用@Inject注释。此类将映射到JCR中的资源。

  • 将组件拖放到页面上并进行配置
    在AEM中使用Sling Models_第3张图片
    在AEM中使用Sling Models_第4张图片

  • 保存组件后,数据将保存在页面节点下,如图所示(存储在JCR中的数据)
    在AEM中使用Sling Models_第5张图片

  • 要在JCR中访问此资源,让我们创建一个Sling Servlet

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

import com.adobe.aem.guides.wknd.core.models.UserModel;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;

/**
 * @author kevin.
 * @Title: rhtj.
 * @Package com.adobe.aem.guides.wknd.core.servlets.
 * @Description: 这个servlet使用HTTP GET方法从RESTful web服务读取数据..
 * @date 2020/04/07 9:22 上午.
 */
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Sling Demo Servlet",
        "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/slingmodel/user"})
public class SlingModelServlet extends SlingSafeMethodsServlet {

    /**
     * uuid.
     */
    private static final long serialVersionUID = 7558680464517017317L;

    /**
     * Logger.
     */
    private static final Logger log = LoggerFactory.getLogger(SlingModelServlet.class);

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {

        try (ResourceResolver resourceResolver = request.getResourceResolver()) {

            log.info("----------< Processing starts >----------");
            //获取资源.
            Resource resource = resourceResolver
                    .getResource("/content/forms/af/wknd/test-dome/jcr:content/guideContainer/rootPanel/items/user");
            //将资源调整为UserModel类.
            UserModel model = resource.adaptTo(UserModel.class);
            //
            response.getWriter()
                    .print("存储在资源中的数据是:\nFirst Name: " + model.getFirstName() + "\nLast Name: "
                            + model.getLastName() + "\nGender: " + model.getGender() + "\nCountry: "
                            + model.getCountry());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }
}
  • 获取资源(根据资源路径获取资源).
    Resource resource = resourceResolver
    .getResource("/content/forms/af/wknd/test-dome/jcr:content/guideContainer/rootPanel/items/user");

  • 将资源调整为UserModel类.
    UserModel model = resource.adaptTo(UserModel.class);

最终效果

在AEM中使用Sling Models_第6张图片

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