Monday, January 7, 2008

Portlet Tutorial - Hello Portlet 2.0 World

This post is a part of the Portlet 2.0 (JSR 286) Tutorial series

As explained in the previous post, a portlet is a pluggable component that can be run inside any compliant portal server. Here is an example of a portlet running inside a portal.


Note how the portlet occupies only a part of the portal page. This is the primary difference between a portlet and a servlet. A portlet is meant to occupy only a part of the web page. And a portal web page consists of multiple, different portlets.

Now, let's learn to write a simple Hello World Portlet. The Portlet API defines an interface called javax.portlet.Portlet. Any portlet you write must implement this interface. One easy way to do so is to extend the javax.portlet.GenericPortlet class. The GenericPortlet already implements the Portlet interface and it is strongly recommended that you always inherit GenericPortlet while writing your own portlets.

Here is our code.


public class HelloWorldPortlet extends GenericPortlet{

public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException{

response.setContentType("text/html");
response.getWriter().write("Hello Portlet 2.0 World");
}
}

We have overridden only one method of the GenericPortlet interface called render.We will look into the render method in detail in a subsequent blog post. For the time being you can assume that whenever your portlet is "shown" on the portal page, the render method is called.

Since portlets are extended components of a regular web application, they can also be packaged along with your servlets and JSPs in a war file. However you will need a separate descriptor along your regular web.xml to describe portlets. This descriptor is called the portlet descriptor and the file is called portlet.xml.

Here is the portlet.xml for our Hello World Portlet.

<portlet-app xmlns=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\"
xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
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\">
<portlet>
<portlet-name>HelloWorldPortlet</portlet-name>
<portlet-class>com.portalzone.example1.HelloWorldPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
<portlet-mode>help</portlet-mode>
</supports>
<portlet-info>
<title>Hello World Portlet</title>
</portlet-info>
</portlet>
</portlet-app>


That’s it ! You have written your first portlet application. You can grab the ready to deploy war file from here: example1.war and deploy the application to your container of choice. If you are interested in the source, here it is : example1-src.zip.

5 comments:

Anonymous said...

Great work. Keep it up !

Anonymous said...

I don't know how to use it!
I want to see it through Exo-Portal and any idea why the portal doesn't shows it, despite the folder is made from the war file during the startup of the portal...

Any ideas?

Anonymous said...

in exo portal, you have to put the war in tomcat/webapps (if you are using tomcat).
Then log in as root, and go to the "application registry" portlet, clic auto import (button on the top right corner of the portlet). You should see the new portlet in the portlet list on the left. After that you have to create a new page or edit an existing page to add the new portlet and make it available from the portal.
Hope it helps.

Anonymous said...

смотреть порно малолеток http://free-3x.com/ секс с молодой парочки free-3x.com/ русское молодежное онлайн порно видео [url=http://free-3x.com/]free-3x.com[/url]

Anonymous said...

For Liferay deployment, I needed to add attribute version="2.0" to the portlet-app tag in portlet.xml .