【问题标题】:How to access suffixes using HTL/Sightly?如何使用 HTL/Sightly 访问后缀?
【发布时间】:2018-05-09 01:10:11
【问题描述】:

TLDR:创建一个名为“mypage.html”的新 AEM 页面。在 URL 中提供后缀。将此后缀传递给 Sling servlet。后缀充当 URL 参数。

示例所需的 URL:http://localhost:4502/mypage.html/john/smith


所以我创建了一个可以读取后缀的 servlet(使用本指南:http://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/)。

@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("/");

如果我通过http://localhost:4502/content/geometrixx/en.name.html/first/last访问它,它工作正常

接下来我要做的是创建一个名为“mypage.html”的新页面,并提供 first 和 last 作为后缀。

mypage 将在格式正确的页面中显示与此人相关的信息。 使用上面的代码,我得到的只是 JSON 响应。

我认为实现目标所需的一些假设/更改:

  1. 我将使用路径和请求参数(即在 servlet 代码上使用 request.getParameter("myparameter"))
  2. 我将使用 AJAX 访问 servlet

如果我的假设是正确的,我如何从 HTL/Sightly 访问后缀?我知道我可以通过 ${request.requestURI} 甚至 Javascript 获取 URI。然后使用这个值,我可以在我的 AJAX 调用中使用它。

但这是 AEM/Sling 的做法吗?或者也许有更好的方法来做我想做的事?

非常感谢!

【问题讨论】:

  • 您可以使用 HTL 中的RequestPathInfo 接口来访问后缀。 ${request.requestPathInfo.suffix}.
  • 是否有一个页面列出了 ${request} 中的所有可用属性?非常感谢!
  • 请作为答案回复,我会接受。谢谢

标签: aem sling sightly htl


【解决方案1】:

您可以使用 HTL 中的RequestPathInfo 接口来访问后缀。 ${request.requestPathInfo.suffix}

可通过 HTL 访问的全局对象 -> here

可通过请求对象访问的方法 -> here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多