DataSource Servlet For Dynamic Drop Down




I am using granite select resourceType to populate list of options. My servlet is written in a way which render different list based upon different parameter.

Below is the sample servlet

@SlingServlet(paths = "/api/bin/dropdown/vivek.json",

    selectors = {"abc","def"},

    extensions = ".html",

    methods = "GET")

public class AvailableThemeServlets extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1668099305241096740L;


    protected void doGet(SlingHttpServletRequest request,

                         SlingHttpServletResponse response) throws ServletException,

        IOException {

        /*String[] selector = request.getRequestPathInfo().getSelectors();

        for (String obj : selector) {



        List<Resource> themes = new ArrayList<Resource>();

        // set fallback

        ResourceResolver resolver = request.getResourceResolver();



        ValueMap vm = null;

        for (int i = 0; i < 5; i++) {

            // allocate memory to the Map instance

            vm = new ValueMapDecorator(new HashMap<String, Object>());

            // Specify the value and text values

            String Value = "value" + i;

            String Text = "text" + i;

            // populate the map

            vm.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);



If I hit this servlet from browser say localhost:4502/api/bin/dropdown/ , via debugging can see executions completes

If this servlet pass via datasource say /api/bin/dropdown/ servlet doesn't execute.


If this servlet pass via datasource say /api/bin/dropdown/vivek.json servlet dynamic options displays in drop down [Expected Response]

Wondering why datasource doesn't working with selectors.


Accepted Solutions (1)

Accepted Solutions (1)




'selectors' won't work with 'path' but with resourceType. Modify your code to remove selectors & extension or change path to resourceType


The request URL selectors supported by the servlet. The selectors must be configured as they would be specified in the URL that is as a list of dot-separated strings such as print.a4. In case this is not empty the first selector(s) (i.e. the one(s) on the left) in the request URL must match, otherwise the servlet is not executed. After that may follow arbitrarily many non-registered selectors. The property value must either be a single String, an array of Strings or a Vector of Strings. This property is only considered for the registration with sling.servlet.resourceTypes.

refer docs -

Apache Sling :: Servlets and Scripts

Answers (0)