0

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

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

我在组件中有 fie options.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

但是在我的对话框中,没有填充值。任何想法。

谢谢

4

1 回答 1

2

它不起作用,因为您${json}将始终是一个空字符串,因为您使用 EL 来显示该值,但从不首先设置该值。

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

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

或者

pageContext.setAttribute("json", json);

为了使您的代码正常工作,您可以直接使用 scriptlet<%= json %>代替 json 输出 json,${ json }也可以最初将值设置为 pageContext 然后使用${ json }

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

<%@ 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);
}
%>

最后,如果您要将整个 json 作为属性保存在某个节点中,则无需编写 json.jsp 来获取值,您可以直接提供保存该值的属性的路径。即,而不是$PATH.options.json您可以直接指定为/etc/IgWebCMS/articletypes/json

于 2014-08-10T18:05:31.067 回答