Tip

How to define a portlet namespace and context paths using JSTL and JSP

Learn how to define portlet namespaces and context paths using JSTL and the JSP expression language.

Living in the portal development world, I'm sure that you're already familiar with the need to namespace unique page elements and the need to use context path to access resources like image, CSS and JS files from within the portlet views. The difficulty is that traditional approaches make the code difficult to read, especially if you followed the archaic method of prefixing your unique entities with the <portlet:namespace/> tag.

I propose an alternative that defines portlet namespaces and portlet context paths using JSTL andJSP> EL.

Author's note: I recently discovered that this so-called archaic method seems to be required by some portal servers (e.g., Liferay 6.2).

Traditional namespace examples

JSP Expression <%=renderResponse.getNamespace()%>
JSTL ${renderResponse.getNamespace()}
Portlet Tag Library <portlet:namespace/>
<form id="<portlet:namespace/>MyFormName$">
<input type="text" id="name$<%=renderResponse.getNamespace()%>" name="name"/>
<input type="Submit" onclick="displayGreeting${renderResponse.getNamespace()}()"/>
</form> <script>
function displayGreeting${renderResponse.getNamespace()} (){
var name =
document.getElementById("name$<%=renderResponse.getNamespace()%>"); alert("Greetings " + name + "!");
}
</script>

Traditional Context Path examples

JSP Expression <%=renderRequest.getContextPath()%>
JSTL ${renderRequest.getContextPath()}
<link rel="stylesheet"
href="<%=renderRequest.getContextPath()%>/css/MyProjectStyling.css"/>  

<script src="${renderRequest.getContextPath()}/js/MyProjectJavascript.js"/>

<img src="${renderRequest.getContextPath()}/images/MyProjectImage.png"/>

Proposed solution

I propose defining two JSTL variables to replace the need for unnecessarily cumbersome code. It should clean your code considerably and make it easier to read and debug.

What are the prerequisites?

First and foremost, we need access in our JSPs to the portlet objects. So we absolutely require, at a minimum, the Portlet Tag Libraries and JSTL Tag Library includes, and we mustn't forget to define the portlet objects. So, here's the code:

<%@ taglib prefix="portlet"
uri="http://java.sun.com/portlet_2_0"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<portlet:defineObjects />

Note: This will be placed at the top of the JSP along with any class and tag library includes, and is already required for the traditional approaches.

How do I define the new portlet namespace and Context Path?

<c:set var="ns" scope="page"
value="${renderResponse.getNamespace()}"/>

<c:set var="cp" scope="page" 
value="${renderRequest.getContextPath()}"/>

What does the code look like?

Context Path Example

<link rel="stylesheet"
href="${cp}/css/MyProjectStyling.css"/> 

<script src="${cp}/js/MyProjectJavascript.js"/>

<img src="${cp}/images/MyProjectImage.png"/>

Namespace example

<form id="MyFormName${ns}">
     <input type="text" id="name${ns}" name="name"/>
     <input type="Submit" onclick="displayGreeting${ns}()"/>
</form>

<script>

     function displayGreeting${ns}(){

           var name =
document.getElementById("name${ns}").value;

           alert("Greetings " + name +
"!");

     }

</script>

What's next?

In my next tip, I'll tackle another common issue: adding portlet namespace support for external JavaScript files and templates.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close