4.8.2 PortletPageListener

This interface provides the opportunity to add content to the top and bottom of the aggregated page. Example 4-17 illustrates a simple implementation of the PortletPageListener interface. It is important to note that content returned from the beginPage method is not placed at the top of the page but rather at the top of the aggregated content as displayed in Figure 4-8.

Example 4-17 PortletPageListener implementation

public class AgendaPortlet extends PortletAdapter implements PortletPageListener {
........
 public void beginPage(PortletRequest request, PortletResponse response) 
     throws PortletException, IOException {
  PrintWriter out = response.getWriter();
  out.println("This page contains my agenda.");
 }
 
 public void endPage(PortletRequest request, PortletResponse response) 
     throws PortletException, IOException {
  PrintWriter out = response.getWriter();
  out.println("End of my agenda.");
 }
}

The resulting page including the top and bottom messages is illustrated in Figure 4-8.

Figure 4-8 beginPage and endPage placements

The beginPage is a convenient method when you need to include JavaScript functions needed by your portlet. However. be very conscious of any content you decide to display in the beginPage method as it may adversely affect the overall aggregation of the page. Furthermore, because the page is aggregated, be sure that any functions or global variables you declare have properly encoded the namespace of the portlet to ensure there are no naming collisions. Use the response.encodeNamespace to do this.

Restriction: The Home.jsp can choose to cancel calls to the PortletPageListener via the <wps:pageRender includeBeginPage="no" includeEndPage="no"> tag. In this case, your beginPage and endPage methods will not be called.


Redbooks
ibm.com/redbooks