|
By Vihang Pathak and Satya Ranjan, December 14, 2006; updated May 4, 2007
|
|
|
This article describes portlet development using the NetBeans Portlet Plugin for the Portlet Container 1.0 Beta software bundled with the Java Application Platform SDK Update 2 and for the Portlet Container 1.0 (FCS - final release) bundled with the Java Application Platform SDK Update 3 Preview 2. This article applies to both Portlet Container 1.0 Beta and Portlet Container 1.0 FCS unless otherwise noted.
Note: Note: Also see the NetBeans Portlet Plugin Release Notes and Installation Instructions for Java Application Platform SDK Update 2 and the NetBeans Portlet Plugin Release Notes and Installation Instructions for Java Application Platform SDK Update 3 Preview 2.
Contents
Preface
Have you ever wished for an easy learning curve for an
upcoming technology? Have you wished to focus on
what your application should do? Have you ever wondered why the adoption
of a decent technology could stop due to the absence of tools around it?
If the answer is yes to these questions, then please read on.
Introducing NetBeans Portlet Plugin
The new NetBeans Portlet Plugin will make portlet development as
easy as writing a normal Java application, with all the features of
portlets made available to you at the click of a mouse.
This article describes how to write that first simple
portlet. Without getting into too much theory, let's get hands-on experience.
Downloads
The distribution of the software required here can be found at:
Prerequisites
This article assumes that the following is already installed:
- NetBeans Portlet Plugin on NetBeans IDE 5.5 or later
- Java Application Platform SDK Update 2 with Portlet Container 1.0 Beta (Enterprise Class Portlet Container) or Java Application Platform SDK Update 3 Preview 2 with Portlet Container 1.0 (FCS - final release)
The NetBeans Portlet Plugin can be used to deploy/undeploy
portlets
on the already installed portlet container with one of the following
scenarios:
- A Portlet Container downloaded and installed on a
GlassFish application server (see Downloads).
- A Portlet Container bundled with the Java Application Platform SDK Update 2/Update 3 Preview 2 release.
For installation instructions, please visit:
- The Java Application Platform SDK Update 2/Update 3 Preview 2 Installation Instructions and the Portlet Container 1.0 Beta and Portlet Container 1.0 (FCS) Installation Instructions
- The NetBeans Portlet Plugin Installation Instructions for Portlet Container 1.0 Beta and Portlet Container 1.0 (FCS)
Configuration Steps
You will need to configure a
portlet container instance inside your NetBeans IDE software.
Please see the Appendix for further
details.
Scope
This document is intended to provide a step-by-step walkthrough on:
-
How to write your first "Hello World" portlet.
-
How to enhance the plugin's generated code and introduce some
other enhancements. The intention is to serve as a guide so that you
can write more complex logic as desired by your application.
Currently, the portlet plugin supports deployment of portlets on:
There will be other
integrations to other portlet containers since the portlet plugin
already has a flexible interface for customizable integration
with other portlet containers.
Portlet Plugin Example
- The following section uses the Hello World Portlet as an example.
Creation
-
Create a New NetBeans Project.
-
Select New Project->Portlet Applications -> JSR 168
Portlet Application.
-
Enter the name of the Portlet (e.g., Hello World) and follow the
self-guided instructions regarding Project Location, Project Folder,
Package
Definition, and other required fields. (Note that J2EE 1.4 should be
selected as the preferred J2EE environment.)
-
After entering all the above information, click on Finish.
Skeleton Walkthrough
This section provides an overview of the basic template of a
portlet application as created by the NetBeans Portal Plugin.
-
Note that the Portlet Plugin creates all the files
required by the JSR 168 specification for portlets.
-
Observe that there are three files created in the Web Pages
folder (view.jsp, edit.jsp, and help.jsp). Please refer to the
JSR
168 specification for more details. In brief, these JSP files help one
define the various modes in which a portlet can execute. And this is
where developers can insert presentation logic to suit their
application needs.
-
Note that there is a Java file created in the source folder with
the package name one has specified. This Java file is the one that
handles the portlet request to handle the presentation application
requests. It imports all the necessary portlet packages that one needs
to start developing a portlet application.
-
Traverse to the web->WEB-INF directory to view the
portlet.xml. This is also precreated by portlet pack. One may modify
this xml and one may wish to view the portlet specifications for more
details in order to correctly modify it.
Deployment/Undeployment
The following section provides an approach to deploy the portlet
application that one just created.
-
Right-click on a project. Click on properties and for category RUN select the
portlet container on which one wishes to deploy the project. The
options can be as of now "Open Source Portlet Container."
-
Right-click on the project. Click on "Clean and Build Project."
Once it is successful, click on "Deploy Project."
-
Go to Runtime->Servers->Open Source Portlet
Container->global->portlets, to view the list of "portlets."
You can either undeploy the portlets or view the portlets at this
point.
Execution
The following section is where one gets to see how the portlet
execution will look. This portlet that has just been created will be
deployed on the portlet container and will use the
portlet container driver to display the portlet (refer to Portlet
Container documentation for more details).
-
Go to Runtime > Servers > Open Source Portlet Container >
Right-click >Click on Admin Tool.
-
It will open the NetBeans configured browser and display the
portlet "Hello World" within the portlet container driver.
Accept a User Name
and Greet the User Name With a Hello
After having created a simple portlet, let's walk through how one
can modify the source code generated and pass parameters across the
jsp and the portlet doView() methods, etc.
For this we will modify the below mentioned sources:
- In "view.jsp,"
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<!-- The portlet specification defines the portlet tag library with the aim of
solving common problems faced by portlet developers.
-->
<portlet:defineObjects/>
<%
String requestParameter = request.getParameter("helloname");
if(requestParameter == null || requestParameter.equals("")) {
%>
<!-- In order to pass parameters and invoke processAction(), i.e.,
Initiate an ActionRequest, the following code renderResponse.createActionURL()
is being used.Creates a portlet URL targeting the portlet. If no portlet mode,
window state, or security modifier is set in the PortletURL, the
current values are preserved. If a request is triggered by the
PortletURL, it results in an action request. The returned URL can be
further extended by adding portlet-specific parameters, portlet
modes, and window states. The created URL will per default not contain
any parameters of the current render request. ProcessAction() is
otherwise invoked when an action is performed via the "Edit"
mode of a portlet. -->
<form name="testForm" method="POST"
action="<%=renderResponse.createActionURL().toString()%>">
First name: <INPUT type="text" name="firstname">
<INPUT type="submit" value="Submit">
</FORM>
<%} else {
%>
<%=requestParameter%>
<a href="<%=renderResponse.createActionURL().toString()%>">Back</a>
<%}%>
|
-
Add a method in *.java file, which has the doView() etc., defined.
public void processAction(ActionRequest request, ActionResponse response) throws
PortletException, IOException {
try {
// Gets the parameter passed from the form.
String name = request.getParameter("firstname");
if(name != null || !name.equals("")) {
response.setRenderParameter("helloname","Hello " + name);
}
}catch(Exception e){
e.printStackTrace();
}
}
|
Follow the same steps as mentioned for building and deployment.
Enter user name and press "submit." The portlet will
appear with a greeting "Hello" to the user name entered
earlier.
Please send feedback to users@portalpack.netbeans.org.
References
Appendix
Configure
Portlet Container in NetBeans
- Select Tools > Server Manager.
- Click Add Server.
- Select "Open Source Portlet Container" from list of servers.
- Give installed Portlet Container Home e.g.,
"$GLASSFISH_HOME/domains/YOUR_DOMAIN/portlet-container." Click next.
- Enter GlassFish Installation Home e.g., "/software/glassfish."
- Enter Domain Dir e.g., "$GLASSFISH_HOME/domains/YOUR_DOMAIN."
- Enter Glassfish, admin password; default password is
"adminadmin."
For Java Application Platform SDK Update 3 Preview 2:
- Select Tools > Server Manager.
- Click Add Server.
- Select "Open Source Portlet Container" from the list of servers.
- Enter GlassFish Installation Home e.g., "/software/glassfish."
- Enter Glassfish admin password. (Default password is "adminadmin") Click next.
- Check Portlet Container Home(PC Home). It should be "$GLASSFISH_HOME/domains/YOUR_DOMAIN/portlet-container." Click Finish.
|
|
Vihang Pathak has been a member of the technical staff at Sun's India Engineering Center in Bangalore since 2004. He is currently engaged in development of some upcoming features in the Portal Server. He also blogs on a variety of topics.
|
Satya Ranjan, a member of the Sun Java System Portal Server engineering team, has been with Sun since 2004. He is currently involved in developing upcoming features in the Portal Server and also leading the Portal Pack project on netbeans.org.
|
|