portlet defineObjects tag extension

portlet defineObjects tag extension

The portlet tag library includes a tag called <portlet:defineObjects/>. This tag establishes three objects; renderRequest, renderResponse and portletConfig for use in included portlet JSP pages. Although you can certainly access some useful information through these objects using the JSTL tag library, accessing scoped variables and the like will require the use of scriptlets. JSTL users are used to having access to scoped variables via the JSTL implicit objects (requestScope, sessionScope, applicationScope etc.).

I have created an extended version of the <portlet:defineObjects/> (see attached) which will additionally make extra portlet related implicit objects available:

Implicit object

Contains

Implicit object

Contains

portletContextScope

Scoped variables from portlet context

portletSessionScope

Scoped variables from portlet session scope

portletSessionApplicationScope

Scoped variables from portlet session application scope

portletRequestScope

Scoped variables from portlet request scope

portletParam

Portlet request parameters as strings

portletParamValues

Portlet request parameters as collections of strings

This was created by combining the classes from Pluto's org.apache.pluto.tags.DefineObjectsTag with JSTL's org.apache.taglibs.standard.lang.jstl.ImplicitObjects. In common with the JSTL implicit objects these new portlet reference objects are made available on a read only basis.

As an example of using the new tag, your portlet might look something like this:

package helloworld; import java.io.IOException; import javax.portlet.GenericPortlet; import javax.portlet.PortletContext; import javax.portlet.PortletSession; import javax.portlet.PortletException; import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; public class HelloWorld extends GenericPortlet { public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException { response.setContentType(request.getResponseContentType()); PortletContext context = getPortletContext(); PortletRequestDispatcher rd = null; // portletSessionScope test request.getPortletSession(). setAttribute("portletSessionScopeTest", "portletSessionScopeTestValue" ); // portletSessionApplicationScope test request.getPortletSession(). setAttribute("portletSessionApplicationScopeTest", "portletSessionApplicationScopeTestValue", PortletSession.APPLICATION_SCOPE ); // portletContextScope test request.getPortletSession(). getPortletContext(). setAttribute("portletContextScopeTest", "portletContextScopeTestValue" ); // portletRequestScope test request.setAttribute("portletRequestScopeTest", "portletRequestScopeTestValue"); rd = context.getRequestDispatcher("/index.jsp"); rd.include(request,response); } }

You could then access the scoped variables you have set using JSTL inside the included JSP page, so a corresponding "index.jsp" might look like this:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://www.ja-sig.org/jasigportlet" prefix="jasigportlet"%> <jasigportlet:defineObjects/> <c:out value="${portletContextScope.portletContextScopeTest}"/><br /> <c:out value="${portletSessionScope.portletSessionScopeTest}"/><br /> <c:out value="${portletSessionApplicationScope.portletSessionApplicationScopeTest}"/><br /> <c:out value="${portletRequestScope.portletRequestScopeTest}"/><br />

To allow writing back to the portlet scoped variables you'd probably need a portlet aware JSTL. With a portlet aware JSTL I'd expect that the JSTL VariableResolver would itself make portlet implicit objects available and therefore it would do away with the need for this tag.