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.
This will create the project structure in the project directory of the GridSphere source directory.
The structure
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.
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.
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.
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>
<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.
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).
I have done the above step, but it doesn't display the HelloWorld portlet. How can I configure the GridSphere for that?