This section introduces you to the concept of creating portlets, starting with a simple portlet that completely modified. Use the section of the Portlet API and Javadoc as a reference.
Creating a simple portlet instance portlet sample set portlet development environment Hello World portlet Compile Java Source package and deploy portlet generation tag Use a persistent portlet message Pass message and track record log refresh portlet cache output parallel portlet Personalization portlet
If you are familiar with WebSphere Portal Family version 2.1 Product development portlet, you should be able to note that the Portlet API has made significant improvements. See migrating portlets for more information.
Create a simple portlet instance
WebSphere Portal includes the following predefined portlets that can be found in the WP_ROOT / Install directory.
Portlet file name file server portlet, providing a static HTML file fileserverportlet.warservlet call portlet, call servlet as a portlet, ServletInvoker.warjsp portlet, serve the JSPJSPSERVERPORTLET.WARCSV viewer, display data in a comma-separated value format arranged in formatted file csv.warrss portlet Display remote URLs that provide data using rich site summary format. Xslt.warocs viewer displays the "Open Content Cindida" channel supply. OcsViewer.war
These portlets provide basic functions (such as providing static HTML services or dynamic JSP file services), so you don't have to write code to your portal. You can create and deploy any number of instances of these portlets by using the management of the portal. See Portlet management for more information.
Serve static content using the portlet file server
The portlet file server displays any HTML files in its path to the portlet window. By default, it displays Test.html in the FILESERVERPORTLET / HTML directory at the WAR file. To other content services, you can add HTML files to the path to the Portlet file server and use portlet management to modify its URL parameter to a new file in the portal management. The root of the URL parameter points to the WAS_ROOT / InstalledApps / Portlet_ID.EAR / FILESERVER.WAR directory, the portlet_id is the unique identifier created in this directory. You can create multiple portlet instances, each of which serves different content from its respective path.
Portlet sample
A set of sample portlets, which demonstrate the functionality of the Portlet API. These portlets are provided by the Bookmark_Samplets.zip file in the wp_root / dev directory. These sample portlets contain the following files:
Bookmark0.war demonstrates how to get, set and remove a specific portlet. However, this portlet does not produce any output. Bookmark1.war Write output to the portal using PortletResponse. Bookmark2.war implements listeners (for example, inserting an HTML output before the portlet tag in the page). Bookmark3.war Retrieves parameters from portletconfig and retrieves properties from PortletData and PortletSetting. Bookmark4.war gets localized text from the resource bundle. Bookmark5.war contains JSP for viewing methods for portlets. Bookmark6.war contains JSPs for editing methods and implementing ActionListener so that users add bookmarks. The following sections describe how to create a simple Hello World portlet, as well as how to compile, package, and deploy this portlet. No file samples for Hello World; you must use the examples provided in text to create it. However, HelloWorld2.War provided in the WP_ROOT / DEV directory, which demonstrates how to use JSP to provide portlet tags.
Set the portlet development environment
Before trying to discuss any classes and samples discussed in this section, you should set an environment that makes written, compile, and test portlets more easily implement. WebSphere Portal product packages include the following development tools:
WebSphere Studio Application Developer version 4.02 supports e-commerce application terminals to the development, testing and deployment of the terminal. The Portal Toolbox Portal Toolbox is inserted into the Application Developer and provides a wizard to create a portlet application project and create a portletAdApter and MVCPortlet class, as well as a template-based portlet (servletinvokerport, jspportlet, and xsltport, jspportlet, and xsltport, JSPportlet, and xsltportlet). Make sure you set your system PATH environment variable to use JDK 1.3.0, which is JDK used by WebSphere Application Server. This JDK level is required to compile class files to use in portlets.
In addition to developing workstations, the portal server should be set to publish and test your portlet application project by installing the following components:
WebSphere Portal WebSphere Application Server - Advanced Edition, Single Server
Hello World Portlet
The Hello World Portlet provides an introduction to your first portlet. Hello World Extension AbstractPortlet Heliers Class and provides a minimum way for portlet. It uses PortletResponse to provide a simple output to the portal page.
Example: Hello World Portlet
Package com.ibm.wps.samples.helloworld;
Import org.apache.jetspeed.portlet. *;
Import org.apache.jetspeed.portlets. *;
Import java.io. *;
Public Class HelloWorld Extends Abstractportletlet
{
Public void init (portletconfig portletconfig) Throws unavailableException
{
Super.init (portletconfig);
}
Public Void Service (PortletRequest portletRequest, PortletResponse portletResponse)
Throws portletException, IOException
{
PrintWriter Writer = portletResponse.getwriter ();
Writer.println ("
Hello Portal World! P>");
}
}
Compilation Java source code
Compile the JAVA source file using the JDK provided by WebSphere Application Server. Before compiling the Java source, set the ClassPath for the compiler to find the JAR file of any portlet package used by the portlet. The following JAR files should always be set in the ClassPath to compile:
WAS_ROOT / LIB / App / Portlet-Api.jar;
WAS_ROOT / LIB / APP / WPSPORTLETS.jar;
WAS_ROOT / LIB / APP / WPS.JAR;
Where WAS_ROOT is the installation directory of WebSphere Application Server. Also, if you need any classes of the servlet function, add the following:
WAS_ROOT / LIB / J2EE.JAR;
WAS_ROOT / LIB / WebSphere.jar;
Then, compile the portlet using the fully qualified path of the Java portlet source code.
Javac-ClassPath% classpath% com.ibm.wps.samples.Helloworld.java
To test the Hello World after compiling, you must first package it into a WAR file and install it to the portal server. However, the first step is to encapsulate the portlet into a JAR file format. To create a JAR file named helloWorld.jar, enter the following command:
Jar -cf helloworld.jar HelloWorld.class
See the JDK document for more information on JAR commands.
Package and deploy portlet
After completing the development and testing portlet, the portlet is ready to deploy to the portal server in the form of a web application archive or WAR file. You also need to package your portlet into a WAR file to test it in the portal server.
The WAR file format contains Java classes and resources that make up a single portlet. The resource can be an image, a JSP file, or an attribute file that contains a message text that is translated. In addition to portlet code and resources, the WAR file also includes web application deployment descriptors (web.xml) and portlet.xml, which contains the information required to install and configure portlets. Package the portlet class, resources, and descriptive information into a single file, making the distribution and deployment of portlet easier.
WebSphere Portal contains a management portlet for installation, uninstall, and updating portlets. The benefits of containing portlets in the WAR file can be dynamically downloaded and installed. The portal administrator can download the WAR file from the Internet and then use the portal management interface to install the portlet to WebSphere Portal. The portlet is ready for use, and does not need to restart the server.
To encapsulate the portlet in the WAR file, follow these steps:
Create a deployment descriptor. Press the WAR file directory structure to arrange the portlet file. Use the JAR utility to encapsulate files.
Hello World deployment descriptor
The following samples can be encapsulated with the Hello World Portlet. See Deploying descriptors for complete information about this topic.
Web application deployment descriptor:
XML Version = "1.0" encoding = "UTF-8"?>
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
servlet>
servlet-maping>
web-app>
Portlet deployment descriptor:
XML Version = "1.0" encoding = "UTF-8"?>
"portlet_1.1.dtd">
cache>
Markup>
supports>
portlet>
portlet-app>
context-param>
logage>
concrete-portlet>
concrete-portlet-app>
portlet-app-def>
WAR file directory structure
Before encapsulating your portlet, you must arrange the class files and resources as described here. The portlet application exists as a structured directory hierarchy.
/
The root directory of the portlet file structure.
/ images
The location of any image required for portlet.
/ Web-inf
All positions of protected resources.
The / web-inflicment is used to store the portlet descriptor documentation required by the package portlet, and the JAR files and classes that can be performed at all runtime.
The portlet information directory is not part of the application utility document tree. The files residing in / web-infers are not directly serving clients.
/ Web-INF / LIB
Store the location of the portlet jar file.
/ jsp
The location of the JSP file.
/ Web-inf / classes
The location of the portlet class file. Individual class files should be stored
/ WEB-INF / CLASSES is reflected in the directory structure of the class package. For example, packages
Com.ibm.wps.samples.helloworld classification
HelloWorld.class should be stored
/Web-inf/classes/com/ibm/wps/samples/helloworld.class
.
/ META-INF
The location of the Manifest file,
MANIFEST.MF. Manifest is defined for the Java 1.3 specification in the standard JAR file format.
The content in / meta-infers is not serving the client.
Multilingual view selection
WebSphere Portal supports the internationalization of portals and portlets. Those JSPs that contain most of the text (for example, help JSP) can be directly translated. This means that the text is included in the JSP instead of the resource bundle. For JSPs that do not use the resource bundle, you need to store it to the appropriate localization. When the portlet uses JSP to the output portlet, the portal will search and select the correct JSP based on the settings of the target browser and the target browser of the country or region. To use the portletcontext.include () function to include the JSP to portlet: getPortletConfig (). GetContext (). Include (Path, PortletRequest, portletResponse);
To make the portlet, you must use the following directory structure to encapsulate the portlet's JSP to the WAR file:
WEB-INF / JSP / MARKUP_TYPE / LANGUAGE _COUNTRY_VARIANT /JSPNAME.JSP
among them:...
path
The path of the developer defines. Path can't be included
MIME-TYPE /
Language_
Country_
Variant. The include () method has positioned the correct JSP in these directories.
Markup_type
Yes
HTML,
WML or
Chtml.
Language
Is a language of JSP, for example
EN,
JA or
DE.
COUNTRY
Is a country or region of JSP, such as US, UK or CA.
Variant
It means different browsers, such as IE5 or NS4.
For example, if the client is used by the language characteristics, the Internet Explorer 5, method includes include (/mypath/mytemplate.jsp, portletRequest, PortletResponse) Let the portal server look for JSPs in the following order.
/html/mypath/ie5/EN_us/mytemplate.jsp /html/mypath/ie5/EN/MyTemplate.jsp /HTML/mypath/ie5/mytemplate.jsp /html/mypath/en_us/mytemplate.jsp / html / mypath / en /mytemplate.jsp /html/mypath/mytemplate.jsp /html/en_us/mytemplate.jsp /html/en/mytemplate.jsp / HtML/Mytemplate.jsp /mytemplate.jsp
Pack portlet and resource into the WAR file
Any JAR utility can be used to build a WAR file. Here's an example of how the JAR utility provided by WebSphere Application Server.
To create a WAR file named mailportlet.par and contain all files in / web-inf and / images: jar -cf helloworld.war images Web-INF
To update an existing WAR file helloworld.war: jar -uf helloworld.war Web-inf / portlet.xml
To extract the portlet descriptor from the WAR file helloworld.war: jar -xf helloworld.war web-inf / portlet.xml
To take all the files from the existing WAR file helloworld.war: jar -xf helloworld.war
Once you have created a WAR file, you can press Portlet to install it to WebSphere Portal. To make portlet applications and complex portlets easy, XML access is a portlet configuration file called for installation. Use the portlet configuration file to allow the specified location and page to place your portlets, topics, and appearances, support tags, and clients, and other settings. This is especially useful for portlets that use messages because these portlets must be placed on the same page. To get more information about XML access, see the XML input and output and the representation of the XML Mural.
Generate marker
In the first example, Portlets provide tags using Java PrintWriter. Most tags are generated using JSP. When the portlet must transform the XML source, there is an exception. In this case, the portlet can use the XSLT to generate tags.
Use JSP to generate tags
One of the easiest ways to divide the portlet marker from the main function of portlet is to use JSP. Below is a JSP for editing pages for Hello World2 samples. Separated views or help JSP will exist to provide user interfaces to support additional portlets.
There are a few considerations when writing your JSP:
To keep the consistency of the portal appearance, use the portlet class specification in the style sheet of the portlet. Make sure to include the JSP tag library for WebSphere Portal to get the features you need to manage the namespace of your portlet. In addition, the request parameter name is set using the
Example: JSP for editing methods for Hello World2
<% @ Taglib Uri = "/ Web-INF / TLD / Portlet.tld" prefix = "portletApi"%>
Configure Hello World portlet span>