在AEM中使用Sling Models

使用Sling Models

示例

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

  • 创建一个新的AEM项目(参考前面博文),然后在您的AEM实例中进行部署。
  • 转到CRXDE,然后在/apps中的项目下使用以下配置创建一个新组件
    在这里插入图片描述
  • 现在,使用以下配置在组件节点下创建dialog节点
    -在这里插入图片描述
    在这里插入图片描述
  • 将代码进行同步到本地
    在这里插入图片描述
  • 现在,使用以下配置在组件节点下创建dialog节点
<?xml version="1.0" encoding="UTF-8"?>
<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中的资源。

  • 将组件拖放到页面上并进行配置
    在这里插入图片描述
    在这里插入图片描述

  • 保存组件后,数据将保存在页面节点下,如图所示(存储在JCR中的数据)
    在这里插入图片描述

  • 要在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);

最终效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程秀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值