How to get currentPagePath in Slingservlet?

被刻印的时光 ゝ 提交于 2019-12-11 02:27:01

问题


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();
...
}

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


回答1:


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

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.

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();
}

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

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

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();
}



回答2:


If you define the servlet with a fixed paths property you don't have any reference to a Resource or Page You either need to define resourceTypes that matches to a page component or use cq:Page, but this will then be active for every request to a page and is not recommended without at least some selectors Then you can get the Resource with request.getResource(). To get a Page you'll need to adapt the ResourceResolver to a PageManager and use getContainingPage(Resource resource).

Have a look at the documentation: http://sling.apache.org/documentation/the-sling-engine/servlets.html




回答3:


request.getPathInfo() in this case presumably is /bin/fooServlet?[parameterString], which is why PageManager is returning null for its path — from the PageManager's point of view, no resource exists at this location.

One simple option would be to send an additional callingPage parameter when hitting the Servlet. This way you could just read it from the parameter map:

GET /bin/fooServlet?foo=bar&callingPage=/en/home.html

void doGet() {
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);    
    String callingPage = request.getParameter("callingPage");
    String callingPagePath = pageManager.getPage(callingPage).getPath();
}



回答4:


I don't know if it's a good pratice, but perhaps you could use the referer.

import java.net.URI;
import java.net.URISyntaxException;

try {
    String currentPagePath = new URI(request.getHeader("referer")).getPath();
} catch (java.net.URISyntaxException e) {
}


来源:https://stackoverflow.com/questions/24738460/how-to-get-currentpagepath-in-slingservlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!