Create 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 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 containing the Struts path through the call to createPortletURIWithStrutsURL.
Create links in a JSP
The preferred method of creating a link in a JSP is to use a tag. This is the most convenient method to assure that the link will be portal-aware and also take into account Struts Modules. The Struts module implementation requires that the link is prefixed with the ModuleConfig's prefix. A typical Struts tag implementation would include logic similar to the following.
ModuleConfig config = (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY); if (config != null) { value.append(config.getPrefix()); }There are times when a link needs to be created in a JSP using Java code. The links created in a JSP must also prefix the ModuleConfig prefix so the links support Struts Modules. PortletApiUtils has a convenience method that can be use to prefix the URL using the ModuleConfig. The method is called addModulePrefix. The following snippet demonstrates creating a link in a JSP.
<%@ page language="java" import="com.ibm.wps.struts.common.PortletApiUtils" %> <% PortletApiUtils portletUtils = PortletApiUtils.getInstance(); %> <% if (portletUtils != null) { String url = "/bean-write.jsp"; // add the module prefix to the url url = portletUtils.addModulePrefix(url, request); Object portletURI = portletUtils.createPortletURIWithStrutsURL(request, url); %> <a href="http://setgetweb.com/p/portal80/<%=portletURI.toString()%>"><bean:write></a> <% } %>
Related
Parent: Struts Portlet Framework