aemslingsightlyhtl

How to access suffixes using HTL/Sightly?


TLDR: Create a new AEM page called "mypage.html". Supply suffixes in the URL. Pass this suffixes to an Sling servlet. The suffixes act as URL parameters.

sample desired URL: http://localhost:4502/mypage.html/john/smith


So I created a servlet (used this guide: http://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/) that can read a suffix.

@SuppressWarnings("serial")
@SlingServlet(paths="geometrixx/components/hompepage", selectors="name", extensions="html",methods="GET", metatype=true)
public class StaffProfileServlet extends SlingAllMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(CourseBookmarkServlet.class);

    @Override
    protected void doGet(final SlingHttpServletRequest request,
                         final SlingHttpServletResponse response) throws ServletException, IOException {
        RequestPathInfo rpi = request.getRequestPathInfo();
        String[] suffixes = rpi.getSuffix().split("/");

and it working fine if I access it via http://localhost:4502/content/geometrixx/en.name.html/first/last

What I want to do next is create a new page called "mypage.html" and supply first and last as suffixes.

mypage will display information relevant to the person in a properly formatted page. With code above, all I get is JSON response.

Some assumptions/changes which I think is needed to achieve my goal:

  1. I will be using paths and using request parameters (i.e. using request.getParameter("myparameter") on servlet code
  2. I will be using AJAX to access the servlet

If my assumptions are correct, how do I access suffixes from HTL/Sightly? I understand I can get the URI via ${request.requestURI} or even Javascript. And using this value, I can then use this in my AJAX call.

But is this the AEM/Sling way of doing it? Or perhaps there is a better way to do what I want?

Thanks a lot!


Solution

  • You can use RequestPathInfo interface from HTL to access suffix's. ${request.requestPathInfo.suffix}

    Global objects accessible through HTL -> here.

    Methods accessible through request object -> here.