Portlet with Action
Overview

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:

Markups
HTML (VIEW)
Language
English

 
1. Setup the project

Goto here to setup the project directory

 
2. Write the Portlet
CapitalPortlet.java
							                    				
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, 
                   IOException
    {
        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.

CapitalActionListener
							                    				
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 );
            }
            else
            {
                if ( !"yaounde".equalsIgnoreCase( capital.trim(  ) ) )
                {
                    event.setReturnCode( ActionEvent.RETURN_ERROR );
                }
                else
                {
                    event.setReturnCode( ActionEvent.RETURN_SUCCESS );
                }
            }
        }
        else
        {
            throw new PortletException( "Unknown action: " + action );
        }
    }
}
    
							                    			

NOTES:

  • 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.

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

<portlet-app>
   <portlet-app-name>Portlet Application</portlet-app-name>
        
   <portlet>
        <portlet-name>capital</portlet-name>
        <portlet-class>net.sf.jportlet.samples.action.CapitalPortlet</portlet-class>
        <action-listener>net.sf.jportlet.samples.action.CapitalActionListener</action-listener>
                
        <default-locale>en</default-locale>
        <language locale="en">
            <title>Capital</title>
        </language>     
        
        <supports>
            <markup name="html">
                <view />
            </markup>
        </supports>
        
        <webflow>
        	<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>
        	</action>
        </webflow>
    </portlet>
</portlet-app>
    
							                    			

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

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

<jportlet:page width="100%" title="Capital">
    <jportlet:body>
        <jportlet:portlet name="capital" returnURI="/action"/>
    </jportlet:body>
</jportlet:page>   
    
							                    			
success.jsp
							                    				
<html>
<head>
    <title>Hello World</title>
</head>
<body>

BRAVO!

</body>
</html>
    
							                    			
error.jsp
							                    				
<html>
<head>
    <title>Hello World</title>
</head>
<body>

Looser. Go back to school to learn your geography!

</body>
</html>
    
							                    			
 
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