Creating link tags in Struts

Get an overview of how to write tags to create links in a JSP in both a servlet and a portlet in Struts framework.

Tags that create links need to create links that are serviced by the portlet. The URL that is created because of the Struts processing needs to be passed as a parameter back to the portlet. There should be a common tag for both a servlet and a portlet. The following demonstrates how to write a tag that can be used by a JSP in both a servlet and a portlet.

PortletApiUtils portletUtils = PortletApiUtils.getInstance();
if (portletUtils != null)
{
   Object pResponse = portletUtils.getPortletResponse((HttpServletRequest)
                                                 pageContext.getRequest());
   Object portletURI =
      portletUtils.createPortletURIWithStrutsURL(pResponse,
                    calculateURL());

   results.append(portletURI.toString() );
}
else
{
   // servlet environment
   results.append(calculateURL());
}

The else clause was the original statement for the servlet-only environment. An instance of PortletAPIUtils is initially obtained. In a non-portlet environment this call would return null. The initialization of the Struts Portlet Framework support would set the instance of the PortletAPIUtils implementation. A PortletResponse object is obtained as an Object. The PortletURI is then created with a DefaultPortletAction and a parameter that contains the Struts path through the call to createPortletURIWithStrutsURL.