Creating Portlets without RAD (IBM WebSphere Portal 8)
This question comes up a lot... Is there any way to create portlets for IBM WebSphere Portal if I do not have RAD available?
However, IBM is showing you how to create a portlet form the command line: Creating a simple portlet. Compiling would require a few Portal libraries. So if you have a portal install available and would like to give it a try, follow the following steps. This is a cheat sheet to the already existing IBM documentation above:
Note: I am assuming that you are on Linux machine and there is a Portal v8 installed. Hard core portal developers that do not need all the bell and whistles of RAD can use the light weight Eclipse IDE (configure the Build Path to include the required libraries required for compilation and create some custom Run Configuration to automate the deployment).
1. Create the directories structure and files for your future HelloWorld portlet:
META-INF/MANIFEST.MF
WEB-INF/classes/com/rebellisconsulting/HelloWorld.java
WEB-INF/portlet.xml
WEB-INF/web.xml
META-INF/MANIFEST.MF
WEB-INF/classes/com/rebellisconsulting/HelloWorld.java
WEB-INF/portlet.xml
WEB-INF/web.xml
2. MANIFEST.MF is a simple manifest file:
Manifest-Version: 1.0
Class-Path:
3. HelloWorld.java would look like this:
package com.rebellisconsulting;
import java.io.*;
import javax.portlet.*;
public class HelloWorld extends javax.portlet.GenericPortlet {
public void init() throws PortletException{
super.init();
}
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
// Display something
response.getWriter().println("HelloWorldPortlet#doView()");
}
}
4. portlet.xml would look like this:
<?xml version="1.0" encoding="UTF-8"?><portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.rebellisconsulting.HelloWorld"> <portlet>
<portlet-name>HelloWorld</portlet-name>
<display-name xml:lang="en">HelloWorld</display-name>
<display-name>HelloWorld</display-name>
<portlet-class>com.rebellisconsulting.HelloWorld</portlet-class> <init-param> <name>wps.markup</name> <value>html</value> </init-param> <expiration-cache>0</expiration-cache>
<supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode>
</supports> <supported-locale>en</supported-locale>
<portlet-info> <title>HelloWorld</title> <short-title>HelloWorld</short-title> <keywords>HelloWorld</keywords> </portlet-info> </portlet>
</portlet-app>
5. web.xml would look like this:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>HelloWorld</display-name>
</web-app>
6. Let's compile the HelloWorld. java:
Set the CLASSPATH for the compiler:
. /opt/IBM/WebSphere/PortalServer/bin/setupCmdLine.sh
Go inside the WEB-INF/classes/com/rebellisconsulting directory and execute:
/opt/IBM/WebSphere/AppServer/java/bin/javac -classpath /opt/IBM/WebSphere/PortalServer/doc/compile/portletapi_20.jar HelloWorld.java
You should have these directories and files now:
META-INF/MANIFEST.MF
WEB-INF/classes/com/rebellisconsulting/HelloWorld.java
WEB-INF/classes/com/rebellisconsulting/HelloWorld.class
WEB-INF/portlet.xml
WEB-INF/web.xml
WEB-INF/classes/com/rebellisconsulting/HelloWorld.java
WEB-INF/classes/com/rebellisconsulting/HelloWorld.class
WEB-INF/portlet.xml
WEB-INF/web.xml
7. Package the portlet into a WAR archive:
/opt/IBM/WebSphere/AppServer/java/bin/jar -cf HelloWorld.war META-INF WEB-INF
8. Deploy your portlet via GUI or use XMLAccess.
9. Voila!
Excellent post, thanks! I can't wait to try this out...
ReplyDelete