簡介

與按資源型別繫結相比,按路徑繫結servlet有幾個缺點,即:

  • 無法使用預設JCR存放庫ACL來控制路徑繫結的servlet的存取許可權
  • 路徑繫結的servlet只能註冊到路徑而非資源型別(即沒有尾碼處理)
  • 如果路徑繫結的servlet不是作用中(例如,如果組合遺失或未啟動),POST可能會導致非預期的結果。 通常會在/bin/xyz建立節點,接著覆蓋servlet路徑繫結
    對於只檢視存放庫的開發人員而言,對應並不透明
    考慮到這些缺點,強烈建議將servlet繫結到資源型別而不是路徑

建立Servlet

在IntelliJ上啟動您的aem-banking專案。 在servlet資料夾下建立名為GetFieldChoices的servlet,如下方熒幕擷取畫面所示。
選擇

範例Servlet

下列servlet繫結至Sling資源型別: azure/fetchchoices

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;
import javax.servlet.Servlet;
import java.io.IOException;
import java.io.Serializable;

@Component(
        service={Servlet.class }
)

        @SlingServletResourceTypes(
                resourceTypes="azure/fetchchoices",
                methods= "GET",
                extensions="json"
                )


public class GetFieldChoices extends SlingAllMethodsServlet implements Serializable {
    private static final long serialVersionUID = 1L;
    private final  transient Logger log = LoggerFactory.getLogger(this.getClass());




    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {

        log.debug("The form path I got was "+request.getParameter("formPath"));

    }
}

在CRX中建立資源

  • 登入您的本機AEM SDK。
  • 在內容節點下建立型別為cq:Page且名為fetchchoices的資源(您可以隨意命名此節點)。
  • 儲存您的變更
  • 建立名稱為jcr:content且型別為cq:PageContent的節點並儲存變更
  • 將下列屬性新增至jcr:content節點
屬性名稱
屬性值
jcr:title
公用程式Servlet
sling:resourceType
azure/fetchchoices

sling:resourceType值必須與servlet中指定的resourceTypes="azure/fetchchoices相符。

您現在可以透過在資源的完整路徑請求sling:resourceType = azure/fetchchoices來叫用您的servlet,並在Sling servlet中註冊任何選擇器或副檔名。

http://localhost:4502/content/fetchchoices/jcr:content.json?formPath=/content/forms/af/forrahul/jcr:content/guideContainer

路徑/content/fetchchoices/jcr:content是資源的路徑,副檔名.json是servlet中指定的路徑

同步您的AEM專案

  1. 在您最愛的編輯器中開啟AEM專案。 我已使用intelliJ來做這件事。
  2. \aem-banking-application\ui.content\src\main\content\jcr_root\content下建立名為fetchchoices的資料夾
  3. 以滑鼠右鍵按一下「fetchchoices」資料夾並選取「repo | Get Command」(此功能表專案是在本教學課程的上一章中設定的)。

這應該會將此節點從AEM同步至您的本機AEM專案。

您的AEM專案結構應如下所示
resource-resolver
使用以下專案更新aem-banking-application\ui.content\src\main\content\META-INF\vault資料夾中的filter.xml

<filter root="/content/fetchchoices" mode="merge"/>

您現在可以使用Cloud Manager將變更推送到AEM as a Cloud Service環境。

後續步驟

啟用Forms Portal元件

recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69