【问题标题】:In CQ5/AEM how does components' Item Load Path work behind the scene?在 CQ5/AEM 中,组件的项目加载路径如何在幕后工作?
【发布时间】:2016-01-18 03:58:47
【问题描述】:

在 CQ5 中,项目加载路径字段允许作者提供一个 URL,从该 URL 加载下拉列表/复选框组等中可用的选项。但它实际上是如何工作的?

编辑:我使用的是 5.6.1 版本,以防万一

以下是我所知道的:

1) 我知道 CQ5 中的组件对话框将其内容存储在:

/content/page/jcr:content/<component node>

在这种情况下,它存储在一个名为 './optionLoadPath' 的属性中:

2)我知道在任何组件(例如,复选框组件)中,要显示的值列表通过以下方式加载到字符串列表中:

编辑 2:@awadheshv 刚刚指出要显示的值列表实际上是使用 FormsHelper.getOptions() 加载的。如果是这样,getValueAsList 是做什么的?

final List<String> values = FormsHelper.getValuesAsList(slingRequest, resource);

在使用 forloop 显示之前:

for (String v : displayValues.keySet()) {
    final String t = displayValues.get(v);
    final String currentId = id + "-" + i;

%><div class="form_row"><%
    LayoutHelper.printTitle(currentId, t, false, true, out);
    %><div class="form_rightcol"><%

            String checked = "";
            if (values.contains(v)) {
                checked = " checked=\"checked\"";
            }
        %><input class="<%= FormsHelper.getCss(properties, "form_field form_field_checkbox") %>" type="checkbox"
                 id="<%= StringEscapeUtils.escapeHtml4(currentId) %>" name="<c:out value="<%= name %>"/>"
                 value="<c:out value="<%= v %>"/>" <%= checked %> />&nbsp;<c:out value="<%= t %>" />
    </div>
</div><%

    i++;
}

3) 我也知道 FormsHelper.getValuesAsList 只是调用 FormsHelper.getValues 并将返回的数组转换为列表。

public static List<String> getValuesAsList(final SlingHttpServletRequest request, final Resource elementResource) {
    final String[] values = getValues(request, elementResource);
    if ( values == null ) {
        return Collections.emptyList();
    }
    return Arrays.asList(values);
}

但我不明白一切是如何结合在一起的。在什么时候/如何将 optionLoadPath 中的项目列表传递给 FormsHelper?

此外,如果我想在默认情况下加载值,我该怎么做呢?例如,假设有一个国家列表我想加载到组件中。我仍然希望用户能够根据需要将 URL 更改为不同的国家/地区列表,但我希望在将组件放入页面后立即加载默认列表。我该怎么做呢?

【问题讨论】:

    标签: aem jcr sling


    【解决方案1】:

    我不认为 loadOptions 是通过 getValues 获取的。 FormsHelper 类有一个单独的方法 getOptions 从 jcr 获取 optionsLoadPath 属性;实现如下-

    public static Map<String, String> getOptions(SlingHttpServletRequest request, Resource elementResource)
      {
        ValueMap properties = ResourceUtil.getValueMap(elementResource);
    
        String[] options = null;
        String loadPath = (String)properties.get("optionsLoadPath", "");
        if (loadPath.length() > 0)
        {
          Resource rsrc = request.getResourceResolver().getResource(loadPath);
          if (rsrc != null) {
            options = (String[])rsrc.adaptTo([Ljava.lang.String.class);
          }
        }
    
        if (options == null) {
          options = (String[])properties.get("options", [Ljava.lang.String.class);
        }
    
        if (options == null) {
          return null;
        }
    
        Map splitValues = new LinkedHashMap();
        for (int i = 0; i < options.length; ++i) {
          String value = options[i].trim();
          if (value.length() > 0) {
            boolean endLoop = true;
            int pos = -1;
            int start = 0;
            do {
              pos = value.indexOf(61, start);
    
              if ((pos > 0) && (value.charAt(pos - 1) == '\\')) {
                start = pos + 1;
                endLoop = false;
              } else {
                endLoop = true; }
            }
            while (!(endLoop));
            String t;
            if (pos == -1) {
              String v = value;
              t = value;
            } else {
              v = value.substring(0, pos);
              t = value.substring(pos + 1);
            }
            String v = v.replace("\\=", "=");
            String t = t.replace("\\=", "=");
            splitValues.put(v, t);
          } else {
            splitValues.put("", "");
          }
        }
        if (splitValues.size() == 0) {
          return null;
        }
        return splitValues;
      }
    

    组件代码会显式调用 getOptions。例如-

     Map<String, String> displayValues = FormsHelper.getOptions(slingRequest, resource);
    

    第二点 - 您可以将默认值放在 options 属性中。

    【讨论】:

    • 啊,有道理。只是为了澄清一下,当您说“您可以将默认值放在选项属性中”时,您指的是哪个节点的哪个选项属性?以及如何加载默认选项?
    • 它应该在您拥有optionsLoadPath 的同一节点上。而且您不需要编写额外的代码来加载它。如果您查看上面的 getOptions() 代码,它会在 optionsLoadPath 返回 null 时使用 options 作为后备。
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多