在下拉列表中填充值

时间:2014-08-10 15:29:23

标签: cq5 aem day-cq

我的节点中有一个属性,其中包含需要下拉的json格式。

[{"text":"Type1","value":"Type1"},{"text":"Type2","value":"Type2"},{"text":"333","value":"333"}]

我在compoent中有component.json.jsp和component.jsp

<%@include file="/libs/foundation/global.jsp"%><%
    response.setContentType("text/plain");
%><%
try {
        Node parent = resource.getResourceResolver().getResource("/etc/IgWebCMS/articletypes").adaptTo(Node.class);
        String json=parent.getProperty("json").getString();
        System.out.println("options json :::: "+json);
        }
     catch (RepositoryException re) {}
%>
        ${json}

在Stdout.log中,它显示了我:

options json :::: [{"text":"Type1","value":"Type1"},{"text":"Type2","value":"Type2"},{"text":"333","value":"333"}]

在对话框下拉中我提到了options属性:$ PATH.options.json

但在我的对话框中,值不会填充。任何想法。

由于

1 个答案:

答案 0 :(得分:2)

它不会起作用,因为你的${json}总是一个空字符串,因为你使用EL显示值,但从不设置值。

要使用EL,您应该在PageContext中拥有值,可以像这样设置。

<c:set var="json" value="<%= json %>" escapeXml="false" />

pageContext.setAttribute("json", json);

为了让您的代码正常工作,您可以直接使用像<%= json %>而不是${ json }这样的脚本来放置json,或者您可以先将值设置为pageContext,然后使用{{ 1}}

但是如果你试图使用scriptlet,你应该考虑更改代码,因为变量是在try块中声明的,但是在它之外使用。

${ json }

最后,如果要将整个json保存为某个节点中的属性,则可以直接提供保存该值的属性的路径,而不是编写json.jsp来获取值。 即。而不是<%@ include file="/libs/foundation/global.jsp" %> <% response.setContentType("text/plain"); try { Node parent = resource.getResourceResolver().getResource("/etc/IgWebCMS/articletypes"). adaptTo(Node.class); out.print(parent.getProperty("json").getString()); } catch (RepositoryException re) { log.error(re.getMessage, re); } %> ,您可以直接指定为$PATH.options.json

相关问题