Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How to pull the servlet resource and request parameters into HTL script and assign dynamically

Avatar

Level 4

1) Currently i'm providing static URL path as datasource instead of servlet path, i tried the below method from pagination but not working

  1. dataSource: function(done) {
  2.   $.ajax({
  3.   type: 'GET',
  4.   url: '/bin/amica/searchResultsServlet?q=' + 'queryString',
  5.   success: function(response) {
  6.    done(response);
  7.    }
  8.    });
  9. }

2) How to include query parameters in HTL script. For example I need to get the parameter value 'q'' attribute it to queryString dynamically after every request?

<script>

    $('.demo').pagination({

               dataSource: 'http://localhost:8080/bin/amica/searchServlet?q= + 'queryString',

        locator: 'searchResults',

        pageSize: 5,

        prevText: "Previous",

        nextText: "Next",

        callback: function(data, pagination) {

            var html = template(data);

            $('.datacontainer').html(html);

        }

</script>

2 Replies

Avatar

Level 10

What are you trying to do. As far as i can see - you are performing a GET operation to a Servlet from HTL component. Now, what - do you want to write the data returned from the GET operation to the HTL component?

Avatar

Level 8

Hi,

Just sharing few thoughts on above :

1] It is preferred to use servlet resolution by resource type[sling.servlet.resourceTypes] than by path[which you have implemented].

2] For pagination[probably another way without servlet would be to] use the result provided by the querbuilder "searchResult = query.getResult();" in the model where querybuilder is used and get the pagination to work.

3] To get the query parameter in model class, 

    @SlingObject

    private SlingHttpServletRequest request;

searchTerm = request.getParameter("q"); should work.