context url Jahia 8 Jahia 7.3

How to generate the URL to a node and prevent context issues

Question

How to generate the URL to a node and prevent context issues

Answer

When rendering a link to a node in a JSP file, you should always use the <c:url> tag, but depending on how you want to generate this link, you have to specify a context, or not.

Imagine that you want to generate a link to the current node. You can use build the full path manually using the ${url.base} or use the getUrl() on your node.

Generate your URL using the ${url.base}

Here is a way to manually generate the URL to the currentNode:

<c:url var="currentNodeUrl" value="${url.base}${currentNode.path}.html" />

In this example, you don't have to set the context; the c:url tag will add it for you

This method is useful if you want to render a link to a dedicated view, for instance, if you want to generate a link to a "detail" view:

<c:url var="currentNodeUrl" value="${url.base}${currentNode.path}.detail.html" />

Generate your URL using the getURL

But most of the case you simply want to generate the default view to a node, sou you can use the getUrl on your node:

<c:url var="currentNodeUrl" value="${currentNode.url}" context="/"/>

But in this case, you need to set the context to "/" to prevent duplicate context in the URL (the getURL already add context in the URL)