Portal, Express Beta Version 6.1
Operating systems: i5/OS, Linux,Windows |
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.
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 your JSPs:
See the Markup guidelines for more information about good JSP coding practices.
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.
<img src='<%=portletResponse.encodeURL("images/earth.jpg")%>' alt="Earth" />
For standard portlets, you also have to add the context path of the portlet from the request:
Figure 4. Standard portlet JSP with image file<img src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/images/earth.jpg")%>' alt="Earth" />
The String returned by the encodeURL() method returns the relative URL of the content, without the host name and domain information.
The following example shows how an audio file can be included in a JSP for a standard portlet.
Figure 5. Standard portlet JSP with multimedia file<object classid='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/audio/WakeUpSong.mp3")%>' type="audio/wav" width="300" height="18"> <param name="controls" value="smallconsole" valuetype="data"> <param name="autostart" value="true" valuetype="data"> <param name="controller" value="true" valuetype="data"> </object>
The following example shows how an applet can be included in an IBM portlet JSP.
Figure 6. Standard portlet JSP with applet<applet codebase='<%=response.encodeURL("applet")%>' code="MyApplet.class" width="150" height="150"> <param name="timeout" value="3600"> <param name="border" value="5"> <param name="font" value="TimesRoman|BOLD|18"> <param name="bgcolor" value="ffffff"> </applet>
WebSphere Portal Express 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 Express 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
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.
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().
The following example shows how to use JSTL to retrieve translated Strings from a resource bundle in your 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 your portlet's WAR file.
For more information about JSTL tags, see JavaServer Pages Standard Tag Library and JSP Standard Tag Library 1.1 implementation.
The following examples are from the ibmHelloJSP.war sample in the portal_server_root/dev/samples directory.
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); } }