By following this process, we can pass the sample data in json format to a dropdown in dialog.
Step 1 : Create component --> create a node for it
in tab1 --> create "items" node of type cq:WidgetCollection
in items --> create node called "counts" of type "cq:widget"
add these properties to "counts" node as follows
Step 2 : Now we need to define a servlet to expose json as return parameter.
For this create a java class called "JsonDataExtractor.java"
Here it follows,
package com.demo.serv;
/*
*Author SONYC
*
*/
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.servlets.*;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import javax.servlet.ServletException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import org.json.*;
import javax.servlet.Servlet;
@SuppressWarnings({"deprecation", "serial"})
@Component
@Service(Servlet.class)
@Properties(value = {
@Property(name = "sling.servlet.paths", value = "/bin/jsondataextractor.json")
})
public class JsonDataExtractor extends SlingAllMethodsServlet{
private static final Logger log = LoggerFactory.getLogger(JsonDataExtractor.class);
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,IOException {
init(request, response);
}
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,IOException {
init(request, response);
}
@SuppressWarnings("static-access")
private void init(SlingHttpServletRequest request, SlingHttpServletResponse response ){
try
{
log.info("-------------In Json Provider class--------------");
ArrayList jsonAr=new ArrayList();
JSONObject obj=new JSONObject();
String jsonData=null;
for(int i=0;i<=4;i++)
{
obj.put("id",i);
obj.put("value",i);
jsonData = obj.toString();
jsonAr.add(jsonData);
obj=new JSONObject();
}
//Return the JSON formatted data
response.getWriter().write(jsonAr.toString());
}
catch(Exception e)
{
log.info("-------------exception-------------"+e.getMessage());
e.printStackTrace();
}
}
}
Step 3 : Now you are able to get the values as json , and can check by hitting http://localhost:4502/bin/jsondataextractor.json
Step 4 :
Place this below code in your component jsp to get selected value.
<%@include file="/libs/foundation/global.jsp"%>
<%@page import="com.day.cq.wcm.api.WCMMode"%><%
%><%@page session="false" %>
<%
pageContext.setAttribute("counVal", properties.get("count",""));
if ( (WCMMode.fromRequest(request) == WCMMode.EDIT))
{
%>
Please click here to add HTML
<%
}
%>
<c:if test="${not empty counVal}">
<div class="container">
${counVal}
</div>
</c:if>
Hope it helps :)
SonyCharan