如何在Slingservlet中获取currentPagePath? [英] How to get currentPagePath in Slingservlet?

查看:90
本文介绍了如何在Slingservlet中获取currentPagePath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从某些JavaScript中,我用( / bin / fooServlet? + params); 调用以下 Slingservlet

From some javascript I call the following Slingservlet with ("/bin/fooServlet?"+params);

@SlingServlet(paths = "/bin/fooServlet", methods = "GET", metatype = true)
public class FooServlet extends SlingAllMethodsServlet {    
..
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        Session  session = resourceResolver.adaptTo(Session.class);
        Page currentPage = pageManager.getPage(request.getPathInfo());
         String currentPagePath = currentPage.getPath();
...
}

我的问题是:如何获得< FooServlet 中当前页面的code> currentPagePath ?代码中的 currentPagePath 为空。

My Question is: How to get the currentPagePath of the current Page in FooServlet? currentPagePath in the code is null.

推荐答案

如托马斯所说,如果您定义具有固定路径属性的Servlet,就不会引用资源。

As Thomas mentioned, if you define a servlet with fixed paths property, you wouldn't have reference to a Resource.

一种实现此目的的方法是将页面路径和请求一起传递给servlet。同样, CQ.WCM.getPagePath()仅返回 / libs / wcm / core / content / siteadmin 页面是siteadmin,您可能需要稍微调整一下脚本才能访问siteadmin中的选定页面。

One way of achieving this is by passing your page path along with the request to the servlet. Also CQ.WCM.getPagePath() returns only /libs/wcm/core/content/siteadmin, as the current page is siteadmin and you may need to tweak your script a bit in order to access the selected page within siteadmin.

要从siteadmin或页面本身获取页面路径,可以使用以下脚本,然后将值传递给servlet进行进一步处理。

To get your page path either from siteadmin or from the page itself, you can use the following script and then pass the value to your servlet for further processing.

var currentPagePath = null;
/* if accessed via siteadmin */
if(CQ.wcm.SiteAdmin.hasListSelection()) {
    var grid = CQ.wcm.SiteAdmin.getActiveGrid();
    var selections = grid.getSelectionModel().getSelections();

    /*Assuming that you are selecting only one page at a time. */
    currentPagePath = selections[0].id;
} else { /* accessed via page */
    currentPagePath = CQ.WCM.getPagePath();
}

然后可以使用currentPagePath作为参数之一来调用servlet。

And then you can call the servlet with the currentPagePath as one of the parameters.

GET /bin/fooServlet?currentPagePath=' + currentPagePath + '&foo=bar';

更新
上面的代码适用于CQ 5.5 + ,对于较旧的版本,您可以使用它。

UPDATE The above code works fine for CQ 5.5 + , for older versions you can use this.

var currentPagePath = null;
/* if accessed via siteadmin */
if(CQ.wcm.SiteAdmin.hasListSelection()) {
    var grid = CQ.Ext.getCmp(window.CQ_SiteAdmin_id + "-grid");
    if (grid) {
        var selections = grid.getSelectionModel().getSelections();
        currentPagePath = selections[0].id;
    }
} else { /* accessed via page */
    currentPagePath = CQ.WCM.getPagePath();
}

这篇关于如何在Slingservlet中获取currentPagePath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆