Hello World Portlet

Now that you have set up Eclipse, create a portlet in the Eclipse Java perspective. Go to the jetexpress-pa project, click on the org.apache.portals.tutorials package, and create a new Java class BonjourMonde:

You will see a new portlet in Eclipse named BonjourMonde. Go ahead and implement the following methods:

Each one of these methods is associated with a portlet mode. Lets make these methods actually do something. Since we are in the render phase when doView/doEdit/doHelp are called, its probably best to render something. The RenderResponse renders content to the output stream of the portlet. Set the content type on the response, and then print a hello world message using a Java Writer:

@Override
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
{
    response.setContentType("text/html"); 
    response.getWriter().println("<b>Bonjour: View Mode</b>");
}

Repeat the same process for Edit and Help modes.

Now lets edit the file portlet.xml (located in src/main/webapp/WEB-INF), and create a portlet descriptor entry for our portlet. Notice that the <supports> element contains that same portlet modes that we support in our do methods.

<portlet>
    <description>Bonjour Monde Portlet</description>    
    <portlet-name>BonjourMonde</portlet-name>  
    <display-name>Bonjour Monde</display-name>
    <portlet-class>org.apache.portals.tutorials.BonjourMonde</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>
    <supported-locale>en</supported-locale>        
    <portlet-info>
        <title>Bonjour Monde</title>
        <short-title>Bonjour</short-title>
        <keywords>tutorial,bonjour,hello</keywords>
    </portlet-info>
</portlet>
      

Next

Now that you have created a new portlet, let's add a page to hold that portlet.

Previous - Next