我希望获取当前url减去当前引用的文件名.解决方案是在JSP还是CQ5中并不重要.但是,我试图使用后者更多地习惯它.
我正在使用这个文档,但它没有帮助.CQ5文件.我找到的示例检索完整的当前路径,但我不知道如何从中删除文件名:
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getPath()));
%>
<a href="<%=containingPage.getPath() %>.html">Profile</a>
Run Code Online (Sandbox Code Playgroud)
假设您正在访问以下资源URL.
/内容/ mywebsite /英语/ MYNODE
如果您当前的节点是"mynode",并且您希望在没有当前节点的情况下获取url的一部分.
那么最简单的方法是在currentNode(mynode)上调用getParent()
因此,你可以像这样得到你父母的路径.
currentNode.getParent().getPath()会给你"/ content/mywebsite/english /"
完整代码:
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getParent().getPath()));
%>
<a href="<%=containingPage.getPath() %>.html">Profile</a>
Run Code Online (Sandbox Code Playgroud)
一种更简单的方法.
您可以使用currentPage对象来获取父页面.
代码看起来像这样
<a href="<%=currentPage.getParent().getPath()%>.html">Profile</a>
Run Code Online (Sandbox Code Playgroud)
如果您在使用此代码时遇到错误,请检查您是否在页面中包含了global.jsp文件.如下所示.
<%@include file="/libs/foundation/global.jsp"%>
Run Code Online (Sandbox Code Playgroud)