Generating output

View how portlets use JSPs to generate markup, create URLs to portlet resources, support multiple devices, markups, and languages in IBM portlets, and make use of JSTL.

In the previous example, the Hello World portlet provided markup by using a Java PrintWriter. However, most portlets generate output using JSPs. One exception to this is when the portlet has to transform XML source. In this case, the portlet can use XSLT to generate markup. The following sections describe how JSPs are used by portlets.


Use JSPs to generate markup

To separate portlet output from the main functionality of the portlet, use a JSP. Below is the JSP for the view page of the jsrHelloJSP.war sample. All sample portlets are available from the portlet catalog by searching for navcode 1WP10017Z. See Sample portlets for more information.

Figure 1. Example: JSP for Hello JSP portlet (standard)

<%@ taglib uri="http://java.sun.com/portlet" prefix="portletAPI" %>
<%@ page session="false"%>
 
<p class="portlet-font">Hello JSP!</p>

If you are familiar with writing to the IBM portlet API, here are some of the key differences between this standard portlet example and the corresponding Figure 7 .

Separate JSPs would exist to provide the user interface for supporting any additional portlet modes, such as edit or help. The basic portlet wizard in Rational Application Developer allows you to create a portlet that provides JSPs for some of the other modes in which the portlet can be invoked.

The following shows the doView() method provided in the jsrHelloJSP sample.

Figure 2. Example: Java source for Hello JSP portlet (standard)

package com.ibm.wps.samples.jsr;
 
import javax.portlet.*;
import java.io.*;
 
public class HelloJSP extends GenericPortlet {
 
  public void init(PortletConfig portletConfig) throws UnavailableException, PortletException
  {
    super.init(portletConfig);
  }
 
  public void doView(RenderRequest request, RenderResponse response)
                                   throws PortletException, IOException {
    // set return content type
    response.setContentType("text/html");
    PortletContext context = getPortletConfig().getPortletContext();
    context.getRequestDispatcher("/jsp/View.jsp").include( request, response);
  }
 
}

If you are familiar with writing to the IBM portlet API, here are some of the key differences between this standard portlet example and the corresponding Figure 8 are as follows:

There are several points to keep in mind when writing JSPs:

  1. For consistency in portal look and feel, use the portlet's class specifications in the JSR 168/JSR 286 specification.

  2. Be sure to include the appropriate tag library to obtain the needed functionality in JSPs.

  3. Become familiar with the guidelines and best practices for portlet markup. For example, all named elements must be namespace-encoded, using the <portletAPI:encodeNamespace> tag, to avoid clashes with other named elements on the portal page.

  4. Portlet JSPs cannot link directly to resources within the portlet's WAR directory structure.


Create URLs to portlet resources

Portlet JSPs cannot link directly to content (for example, images, applets, other JSPs, or other resources) within the portlet's WAR directory structure. Instead, they have to use the services of the portlet container to create portlet URLs from which the content can be accessed. Use the encodeURL() method of the PortletResponse to access content within the portlet WAR structure. The following examples are used in the View World portlet samples.
ibmViewWorld.war


jsrViewWorld.war

The String returned by the encodeURL() method returns the relative URL of the content, without the host name and domain information.
Multimedia example


Applet example


Support multiple devices, markups, and languages in IBM portlets

WebSphere Portal supports PC browsers, i-mode and WAP phones, plus the ability to create and add support for other devices and clients, markup types, and languages. The challenge with supporting multiple devices, markups, and languages is to render content differently depending on the characteristics of the client, which is provided in the portlet request. For example, one browser may accept HTML 4.0 while another supports XHMTL 1.0. One WAP device might support four lines with 25 characters while another phone has its own PDA-style interface.

For IBM portlets, the aggregation component of WebSphere Portal allows you to package JSPs and resources in directories that match the client with the request, reducing the amount of programming you have to do to support multiple client types. JSPs that contain mostly text, such as help JSPs, can be translated directly rather than storing strings in a resource bundle. JSPs that do not use resource bundles need to be stored in the appropriate location. When a portlet uses a JSP for rendering of the portlet's content, the portal searches for and selects the proper JSP based on the client type (including browser), markup language, and locale indicated in the request. To include a JSP in a portlet, use the appropriate method:

   // JSR 168 include
   context.getRequestDispatcher("/jsp/View.jsp").include( request, response);
 
   // IBM API include
    context.include("/jsp/View.jsp", request, response);

To support multiple markup types and locales, the portlet's JSP's must be packaged in the WAR file using the following directory structure:

jsp_path/markup_type /language _region/client/jspname.jsp

Where
jsp_path


markup_type


language


region


client

For example, if the client is using Internet Explorer 5 with language properties are set to English (United States), the method include(/mypath/mytemplate.jsp, portletRequest, portletResponse) would cause the portal server to look for the JSP in the following order.

  1. /mypath/html/ie5/en_US/mytemplate.jsp

  2. /mypath/html/ie5/en/mytemplate.jsp

  3. /mypath/html/ie5/mytemplate.jsp

  4. /mypath/html/en_US/mytemplate.jsp

  5. /mypath/html/en/mytemplate.jsp

  6. /mypath/html/mytemplate.jsp

  7. /mypath/mytemplate.jsp

Standard portlets must provide their own logic for handling requests from multiple locales and markup types.

Content accessed by a portlet JSP (for example, images or HTML pages) using ServletResponse.encodeURL() are not found by portal aggregation. To provide different versions of these resources based on client type, markup, and locale, the portlet must use PortletContext.include().


Use JSTL in portlet JSPs

The following example shows how to use JSTL to retrieve translated Strings from a resource bundle in JSPs.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
...
<fmt:setBundle basename="nls.reminder"/>
...
<img border='0'
     src='<%=response.encodeURL("task_add.gif")%>'
     title='<fmt:message key="add_reminder"/>'
     alt='<fmt:message key="add_reminder"/>'/>

The JARs required to implement JSTL tags are included with the portal server. You should not package these JARs in portlet's WAR file.


IBM portlet API examples for Hello JSP

The following examples are from the ibmHelloJSP.war sample.

Figure 7. Example: JSP for Hello JSP portlet (IBM)

<%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>
 
<p class="portlet-font">Hello JSP!</p>

Figure 8. Example: JSP for Hello JSP portlet (IBM)

The PortletContext.include() method is used to invoke the JSP for the view mode.

package com.ibm.wps.samples.v4;
 
import org.apache.jetspeed.portlet.*;
import java.io.*;
 
public class HelloJSP extends PortletAdapter {
 
  public void init(PortletConfig portletConfig) throws UnavailableException
  {
    super.init(portletConfig);
  }	
 
  public void doView(PortletRequest request, PortletResponse response) 
                                    throws PortletException, IOException {
 
    PortletContext context = getPortletConfig().getContext();
    context.include("/jsp/View.jsp", request, response);
  }
 
}


Parent

Understand the basics

 


+

Search Tips   |   Advanced Search