Portlet with Action

We are now going to create another portlet that uses actions.
The purpose of this portlet is to ask the user the capital of a country. If the user success, the application will display Bravo, else it will display Looser
This portlet supports:


1. Setup the project

Goto here to setup the project directory

2. Write the Portlet
package net.sf.jportlet.samples.action;

import java.io.IOException;
import java.io.PrintWriter;
import net.sf.jportlet.portlet.PortletAdapter;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.PortletResponse;

public class CapitalPortlet
    extends PortletAdapter
    protected void doView( PortletRequest  request,
                           PortletResponse response )
        throws PortletException, 
        PrintWriter writer = response.getWriter(); 
        writer.println("What's the capital of Cameroon? ");
        writer.println("<input type='text' name='capital'>");
        writer.println("<input type='submit' value='Submit'>");
3. Write the Portlet ActionListener

When the Submit button is pressed, that interaction will be tranformed by the container to an ActionEvent. You must create an ActionListener that will intercept that event.

package net.sf.jportlet.samples.action;

import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.event.ActionEvent;
import net.sf.jportlet.portlet.event.ActionListener;

public class CapitalActionListener
    implements ActionListener
    public void actionPerformed( ActionEvent event )
        throws PortletException
        String action = event.getAction(  );
        if ( ActionEvent.ACTION_VIEW.equals( action ) )
            PortletRequest request = event.getPortletRequest(  );
            String         capital = request.getParameter( "capital" );

            if ( ( capital == null ) || ( capital.trim(  ).length(  ) == 0 ) )
                event.addError( "Please enter the name of the capital" );
                event.setReturnCode( ActionEvent.RETURN_INPUT );
                if ( !"yaounde".equalsIgnoreCase( capital.trim(  ) ) )
                    event.setReturnCode( ActionEvent.RETURN_ERROR );
                    event.setReturnCode( ActionEvent.RETURN_SUCCESS );
            throw new PortletException( "Unknown action: " + action );


  • The action of the event is ALWAYS the name of the portlet's mode.
  • After executing an action, you MUST set the returnCode or nextURI.
    If the returnCode is set, the container will use the <webflow> element of the portlet deployment descriptor to find where to redirect the user. But if the nextURI is set, the container will redirect the user to that URI.

4. Write the deployment descriptors

Now that the portlet are created, you must describe them in the jPortlet deployment descriptor.
The jPortlet deploment descriptor MUST be named portlet.xml and MUST be located in the WEB-INF/ directory of the web application.

<?xml version="1.0"?>
<!DOCTYPE portlet-app SYSTEM "http://jportlet.sourceforge.net/dtd/portlet-1.0.dtd">

   <portlet-app-name>Portlet Application</portlet-app-name>
        <language locale="en">
            <markup name="html">
                <view />
        	<action name="view">
        		<return code="success">/action/success.jsp</return>
        		<return code="error">/action/error.jsp</return>
        		<return code="input">/portlet/capital/mode/view/state/maximized</return>

Note the <webflow> tag, it indicate to the container how to redirect the user after each action is execute.
This tag says:

  • if the view action returns success, redirect the user to /success.jsp
  • if the view action returns error, redirect the user to /error.jsp
  • if the view action returns input, redirect the user to the maximized VIEW mode of the portlet

5. Put the portlet in the Web-application

Now that the portlet is created, you must create all the JSP files required by the application

<%@ taglib uri="/WEB-INF/jportlet.tld"  prefix="jportlet" %>

<jportlet:page width="100%" title="Capital">
        <jportlet:portlet name="capital" returnURI="/action"/>
    <title>Hello World</title>


    <title>Hello World</title>

Looser. Go back to school to learn your geography!

6. Build, deploy and test

Run the following commands from the source directory from PROJECT_HOME/src/:

  1. ant
    This will compile all the files and generate the project .war file
  2. ant deploy
    This will deploy the .war file into the application server.
  3. Navigate to http://localhost:8080/jportlet-action