Reputation: 2704
I wish to get the current url minus the file name that is being currently referenced. Whether the solution is in JSP or CQ5 doesn't matter. However, I am trying to use the latter more to get used to it.
I'm using this documentation but it's not helping. CQ5 Docs. The example I found retrieves the full current path, but I don't know how to strip the file name from it:
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getPath()));
%>
<a href="<%=containingPage.getPath() %>.html">Profile</a>
Upvotes: 3
Views: 5469
Reputation: 9281
A much simpler approach.
You can use the currentPage object to get the parent Page.
The code looks like this
<a href="<%=currentPage.getParent().getPath()%>.html">Profile</a>
In case you are getting an error while using this code, check whether you have included the global.jsp file in the page. The one shown below.
<%@include file="/libs/foundation/global.jsp"%>
Upvotes: 6
Reputation: 10241
Assuming you are accessing the following resource URL.
/content/mywebsite/english/mynode
if your current node is "mynode" and you want to get the part of url without your current node.
then the simplest way to do is, call getParent() on currentNode(mynode)
therefore, you can get the path of your parent like this.
currentNode.getParent().getPath() will give you "/content/mywebsite/english/"
full code :
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getParent().getPath()));
%>
<a href="<%=containingPage.getPath() %>.html">Profile</a>
Upvotes: 6
Reputation: 2330
I don't know anything about CQ5, but since getPath()
returns an ordinary Java string I expect you could just take the prefix up to the last slash, which for a string s
can be done with s.substring(0, s.lastIndexOf('/')+1)
. If you have to make it into a one-liner, you could do containingPage.getPath().substring(0, containingPage.getPath().lastIndexOf('/')+1)
.
Upvotes: -1