Dynamic Value in DropDown in Page Properties With Json URL
I have Create a servlet which is returning JSON data like this:
[
{
"jcr:primaryType":"nt:unstructured",
"value":"option1",
"text":"option1"
},
{
"jcr:primaryType":"nt:unstructured",
"value":"option12",
"text":"option12"
},
{
"jcr:primaryType":"nt:unstructured",
"value":"option123",
"text":"option123"
}
]
I want to use Dropdown in page properties which will dynamically populate itself with data from JSON servlet
endpoint of JSON: /app/authordata.json
in .cq_dialog.xml
<%authorName
cq:showOnCreate="{Boolean}true"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldDescription="Get Author Name"
fieldLabel="Author Name"
name="./authorName">
<datasource
jcr:primaryType="nt:unstructured"
sling:resourceType="x/datasources/dropdown"
options="/app/authordata.json"
repoSource="{Boolean}true"/>
<%authorName>
But the data return empty or null
here is my Servlet
package com.x.core.servlets;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.framework.Constants;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import com.x.core.models.AuthorsListPass;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Author Names DataSource Servlet",
ServletResolverConstants.SLING_SERVLET_PATHS + "=/apps/authordata.json",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET
})
public class AuthorListDataSourceServlet extends SlingSafeMethodsServlet {
@Reference
private ModelFactory modelFactory;
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
// Use the request's resource to adapt to the AuthorsListPass model
AuthorsListPass authorsListPass = request.getResource().adaptTo(AuthorsListPass.class);
if (authorsListPass == null) {
response.getWriter().write("['Ash Didn't Find Anything']");
return;
}
List<String> authorNames = authorsListPass.getAuthorNames();
JsonArray options = new JsonArray();
for (String authorName : authorNames) {
JsonObject option = new JsonObject();
option.addProperty("value", authorName);
option.addProperty("text", authorName);
options.add(option);
}
response.getWriter().write(options.toString());
}
}
can anyone suggest to me how I can achieve that? Need a Super Urgent Solution.
Adobe Experience Manager, Version 2023.1.1091
Thanks