DataSource Servlet For Dynamic Drop Down

vdhim23

25-02-2019

Hi,

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;

    @Override

    protected void doGet(SlingHttpServletRequest request,

                         SlingHttpServletResponse response) throws ServletException,

        IOException {

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

        for (String obj : selector) {

            System.out.println(obj);

        }*/

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

        // set fallback

        ResourceResolver 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 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/vivek.json.abc.html , via debugging can see executions completes

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

1699710_pastedImage_0.png

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.

Regards

Accepted Solutions (1)

Accepted Solutions (1)

Gaurav-Behl

MVP

26-02-2019

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

sling.servlet.selectors

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)