Skip to main content
Developer Jahia 8.2

In a JSP view, how do I test login state, build a node URL, and share constants across views?

Question

In a JSP view, how do I test login state, build a node URL, and share constants across views?

Answer

Login state together with a session attribute

Nothing Jahia-specific is needed - renderContext is an ordinary EL object, so plain JSTL works:

<c:if test="${renderContext.loggedIn and sessionScope.myAttribute eq 'expected'}">
  ...
</c:if>

renderContext.loggedIn is false for the guest user. Remember that a view is rendered server-side and may be cached, so a condition on a per-user session attribute needs the surrounding fragment to be cached per user, or not cached.

Node URLs: pick the right one

Jahia exposes a url object. The two that get confused are:

ELWhat it gives
${url.current}the URL of the resource currently being rendered - inside a component view, that is the component, not the page
${url.mainResource}the URL of the page (the main resource), as base + path + .template.html
${url.base}the base URL for the current workspace and language

So in a component view, ${url.current} is usually not what you want for "the page I am on" - that is ${url.mainResource}.

For an arbitrary node, build it from the base:

<c:url var="nodeUrl" value="${url.base}${myNode.path}.html"/>
<a href="${nodeUrl}">...</a>

Also available: ${url.live}, ${url.edit}, ${url.preview}, ${url.context}, ${url.files}, ${url.logout}, and ${url.language['fr']} for the same resource in another language.

Constants shared across views

Jahia ships a tag for exactly this. Put your constants in a Java class in your module as public static final fields, then expose them to EL:

<%@ taglib prefix="utility" uri="http://www.jahia.org/tags/utilityLib" %>

<utility:useConstants var="myConstants"
                      className="org.example.mymodule.MyConstants"
                      scope="application"/>

<c:if test="${myNode.primaryNodeTypeName eq myConstants.THING_TYPE}">
  ...
</c:if>
Attribute
varrequired - the EL variable the constants map is bound to
classNamerequired - the fully qualified class whose public static fields are exposed
scopeoptional - page (default), request, session or application

The tag reads the class's public static constants into an unmodifiable, case-insensitive map. Using scope="application" means it is resolved once rather than per render.

This is the supported alternative to repeating literals in every view, or to <%@ include %> of a fragment of <c:set> declarations.

Two practical notes:

  • The class must be visible to your module's classloader - putting it in the module that contains the views is the simple case.
  • No JSP shipped in Jahia core uses this tag, so you will not find an in-tree example to copy; the tag and its contract are nonetheless part of the core taglib.

This article was drafted with AI assistance, then reviewed and curated by Jahia Customer Support engineers before publication.