Dashboard > GridSphere Portal v3.0 > ... > Portlet Development Guide > Hello World Portlet
  GridSphere Portal v3.0 Log In   View a printable version of the current page.  
  Hello World Portlet
Added by Oliver Wehrens, last edited by Oliver Wehrens on May 30, 2007  (view change)
Labels: 
(None)

Environment Setup

GridSphere provides some mechanism to get started with portlet programming. In the GridSphere source directory you can use the command ant new-project to start a process at which end you will get a subdirectory containing some template files and a basic structure for your portlets.

ant new-project
Buildfile: build.xml

setenv:
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects
     [echo] Installing for Tomcat
     [echo] GridSphere Webserver is tomcat
     [echo] GridSphere will be deployed to /usr/local/apps/gs3/tomcat as the gridsphere web application

new-project:

new-project:
     [echo] Creating a New Portlet Project
    [input] Please enter a Project Title e.g. Cool Portlets
GS Examples
    [input] Please enter a Project Name this will be used for your portlet web application and should be lowercase e.g. coolportlets
gsexamples

check-project-exists:
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/src
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/lib
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF/classes
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF/classes
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF/persistence
     [copy] Copying 3 files to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF/persistence
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF
     [copy] Copying 1 file to /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/WEB-INF
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/html
    [mkdir] Created dir: /Users/wehrens/Projects/gs/svn/gridsphere/projects/gsexamples/webapp/jsp
     [echo] Creation of new portlet project GS Examples in projects/gsexamples is complete.
     [echo] Please edit src/ webapps/ and webapps/WEB-INF/{web.xml, PortletServices.xml} appropriately
     [echo] Please place portlets in src/**/portlets/ directory and service in src/**/services directory for proper compilation!

BUILD SUCCESSFUL
Total time: 15 seconds

This will create the project structure in the project directory of the GridSphere source directory.

The structure

> ls -l projects/gsexamples/
total 48
-rw-r--r-- 1 wehrens wehrens 467 May 16 17:49 build.properties
-rw-r--r-- 1 wehrens wehrens 17829 May 16 17:49 build.xml
drwxr-xr-x 2 wehrens wehrens 68 May 16 17:49 lib
drwxr-xr-x 2 wehrens wehrens 68 May 16 17:49 src
drwxr-xr-x 5 wehrens wehrens 170 May 16 17:49 webapp

Change to the projects/gsexample directory and have a look at the files.

build.properties
This file was created during the previous process and contains your input as well as some other
settings.

Property Default Description
project.title GridSphere Examples title for the project, will be used for some descriptive text in the layout.xml file
project.name gsexamples The project name is used to determine the name of the webapp this portlet package should be deployed as.
gridsphere.home ../../ Defines where GridSphere source home is located
gridsphere.build ../../build Defines where the GridSphere build files are project.version 1.0 RC 1 Defines a version tag for your project. You can/should change that.
project.build build Defines where the compiled classes go.
project.dist dist Defines where the portlet distribution files goes.
project.priority   If there are any dependencies, an integer must be specified here which will determine the ordering for when this app gets starting from smallest to largest- leave empty if ordering is not important
project.shared   if this project provides any portlet services used by another portlet application, then the value
should be set to true

Once this file is generated you normally don't need to edit it (besides the optional project.version
property).

build.xml
This file normally does not need to be changed. It contains all targets for the default code structure to compile and deploy your code to GridSphere.

lib
If you have 3rd party libraries to be used in your project you need to place them in that directory.

src
All your java based source code should go into that directory. One convention regarding the naming of the packagestructure must be followed if you want to use the the supplied build script. All portlets should be placed in a subdirectory named portlets (e.g. gsexamples/portlets). If you want to use the GridSphere portlet service model you have to place your service interfaces and implementations in a subdirectory named services (e.g. gsexamples/services).

webapp

The webapp directory contains three directories.

> ls -l webapp/
total 0
drwxr-xr-x 10 wehrens wehrens 340 May 18 13:12 WEB-INF
drwxr-xr-x 2 wehrens wehrens 68 May 16 17:49 html
drwxr-xr-x 2 wehrens wehrens 68 May 16 17:49 jsp

webapp/WEB-INF
This is the normal WEB-INF directory of your portlet specification. Two files are needed to be there, web.xml and portlet.xml. Those are defined in JSR 168 and are required. The other files are GridSphere specific and ease the development and deployment of portlets to the GridSphere portal framework.

s -l webapp/WEB-INF/
total 24
-rw-r--r--   1 wehrens  wehrens   770 Jan  3 15:21 PortletServices.xml
drwxr-xr-x   3 wehrens  wehrens   102 Jan  3 15:21 classes
drwxr-xr-x   3 wehrens  wehrens   102 Jan  3 15:21 persistence
-rw-r--r--   1 wehrens  wehrens  3857 Jan  3 15:21 portlet.xml
-rw-r--r--   1 wehrens  wehrens  2112 Jan  3 15:21 web.xml

Programming

Now that we have our project setup we can go ahead and develop some portlets.

Hello World

First we will create a simple Hello World Portlet using JSR 168 standard methods. Create a subdirectory src/org/gridsphere/gsexamples/portlets and create the File HelloWorld.java.

HelloWorld.java
package org.gridsphere.gsexamples.portlets;

import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.PrintWriter;
import java.io.IOException;

/**
 * a simple HelloWorld Portlet
 */
public class HelloWorld extends GenericPortlet {

    public void doView(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h1>Hello World</h1>");
    }

}

Now we need to modify portlet.xml so that GridSphere recognizes the portlet (or any other JSR compliant portlet container). We do already have a template portlet.xml in the webapp/WEB-INF directory deployed. Please replace the current portlet section with the follwing entry:

portlet.xml
<portlet>
     <description xml:lang="en">
        The classic Hello World example
     </description>
     <portlet-name>HelloPortlet</portlet-name>
     <display-name xml:lang="en">Hello World</display-name>
     <portlet-class>
        org.gridsphere.gsexamples.portlets.HelloWorld
     </portlet-class>
     <expiration-cache>60</expiration-cache>
     <supports>
         <mime-type>text/html</mime-type>
         <portlet-mode>edit</portlet-mode>
         <portlet-mode>help</portlet-mode>
     </supports>
     <supported-locale>en</supported-locale>
     <portlet-info>
         <title>Hello World</title>
         <short-title>Hello World</short-title>
         <keywords>hello</keywords>
     </portlet-info>
 </portlet>

This defines the HelloWorld portlet with the portlet name of HelloPortlet (line 5), the portlet class beeing our HelloWorld example (line 8), a cachetime of 60 seconds (line 10), an edit (line13) and help (line 14) mode. Furthermore some information about the portlet is provided (line 17-21).

Now we are ready to compile and deploy the Hello World Portlet.

> ant install
Buildfile: build.xml
setenv:
[mkdir] Created dir: gridsphere/projects/gsexamples/build
...
BUILD SUCCESSFUL
Total time: 7 seconds

After the deployment of the portlet to the portal and configuring GridSphere to display it (go to Layout in the upper right corner and add it the desired layout), you will see your HelloWorld portlet.

Source code

Get the full source code for this example: gsexamples.zip. Unzip this into the projects directory (you maybe have to create this directory first).

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.8 Build:#525 Aug 08, 2006) - Bug/feature request - Contact Administrators