There are many occasion where we need to populate drop down value dynamically. There is already example provide in AEM in list component at location /libs/foundation/components/list. In this example a folder "displayasdatasource" is create and under the folder a JSP "displayasdatasource" has logic to set dropdown value.
AEM uses Datasource to hold the dynamic value . we need to create a map which have dropdown value and add that map to datasource.
In this blog we will write the same logic in servelet and will invoke the servelet from dropdown node. Below are the steps:-
1) Create a component in Touch UI and created a field drop down field in dialog as shown in below pic.
2) Write the servlet to populate the datasource as below.
Note :- Please add "/" before bin in your servlet path otherwise it will not able to locate the servlet. Ex - /bin/availableThemes
4.
Please let me know if you face any issue.
AEM uses Datasource to hold the dynamic value . we need to create a map which have dropdown value and add that map to datasource.
In this blog we will write the same logic in servelet and will invoke the servelet from dropdown node. Below are the steps:-
1) Create a component in Touch UI and created a field drop down field in dialog as shown in below pic.
2) Write the servlet to populate the datasource as below.
3) When your dialog load then it will call the above servlet and populate the dropdown note.package com.aemnote.service.core.servlets;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import javax.servlet.ServletException;import org.apache.felix.scr.annotations.sling.SlingServlet;import org.apache.sling.api.SlingHttpServletRequest;import org.apache.sling.api.SlingHttpServletResponse;import org.apache.sling.api.resource.Resource;import org.apache.sling.api.resource.ResourceMetadata;import org.apache.sling.api.resource.ResourceResolver;import org.apache.sling.api.resource.ValueMap;import org.apache.sling.api.servlets.SlingSafeMethodsServlet;import org.apache.sling.api.wrappers.ValueMapDecorator;import com.adobe.granite.ui.components.ds.DataSource;import com.adobe.granite.ui.components.ds.EmptyDataSource;import com.adobe.granite.ui.components.ds.SimpleDataSource;import com.adobe.granite.ui.components.ds.ValueMapResource;@SlingServlet(paths = "/bin/availableThemes")public class AvailableThemesServlet extends SlingSafeMethodsServlet {private static final long serialVersionUID = 1668099305241096740L;@Overrideprotected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {List<Resource> themes = new ArrayList<Resource>();// set fallbackResourceResolver resolver = request.getResourceResolver();request.setAttribute(DataSource.class.getName(),EmptyDataSource.instance());ValueMap vm = null;for (int i = 0; i < 5; i++) {// allocate memory to the Map instancevm = new ValueMapDecorator(new HashMap<String, Object>());// Specify the value and text valuesString Value = "value" + i;String Text = "text" + i;// populate the mapvm.put("value", Value);vm.put("text", Text);themes.add(new ValueMapResource(resolver, new ResourceMetadata(),"nt:unstructured", vm));}DataSource dataSource = new SimpleDataSource(themes.iterator());request.setAttribute(DataSource.class.getName(), dataSource);}}
Note :- Please add "/" before bin in your servlet path otherwise it will not able to locate the servlet. Ex - /bin/availableThemes
4.
Please let me know if you face any issue.