Search code examples
aemday-cq

Populate Values in Drop Down


I have node in which has a property which contains the json format that required to drop down.

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

I have fie options.json.jsp within the compoent along with 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}

In Stdout.log it shows me :

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

And in the dialog drop down i mentioned the options property as : $PATH.options.json

But in my dialog that values are not getting populate. Any idea.

Thanks


Solution

  • It won't work because your ${json} would always be an empty String, as you are using EL to display the value, but never setting the value in the first place.

    To use EL, you should be having the value in the PageContext, which can be set like this.

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

    or

    pageContext.setAttribute("json", json);
    

    Coming to make your code work, you can either out put the json directly using a scriptlet like <%= json %> instead of ${ json } or you can set the value to pageContext initially and then print it using ${ json }

    But in case you are trying to using scriptlet, you should consider changing the code as the variable is being declared within the try block, but used outside it.

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

    And finally, if you are going to save the entire json as a property in some node, then instead of writing a json.jsp to fetch the values, you can directly provide the path to the property holding the value. ie., instead of $PATH.options.json you can directly specify as /etc/IgWebCMS/articletypes/json