Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

DataSource Servlet For Dynamic Drop Down

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

'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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

'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

Avatar

Level 3

Can we add params to the url?
eg.

<datasource
jcr:primaryType="nt:unstructured"
sling:resourceType="/test/run?a=hi"/>