ProcessAction annotation

The JSR 286 interface introduces concept of ProcessAction annotation that you can use for marking a method that can be used for handling particular action. I built a simple portlet to demonstrate how you can use @ProcessAction annotation, you can download the sample portlet from here

This is how my ProcessActionAnnotationPortlet.java looks like

package com.webspherenotes.portlet.jsr286;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class ProcessActionAnnotationPortlet extends GenericPortlet{

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ProcessActionAnnotationPortlet.doView()");
response.setContentType("text/html");

getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting ProcessActionAnnotationPortlet.doView()");
}

@ProcessAction(name="action1")
public void handleAction1(ActionRequest request, ActionResponse response)throws PortletException,IOException{
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction1()");
System.out.println("Value of ACTION_NAME " + request.getParameter(ActionRequest.ACTION_NAME));
response.setRenderParameter("action", "action1");
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction1()");
}

@ProcessAction(name="action2")
public void handleAction2(ActionRequest request, ActionResponse response)throws PortletException,IOException{
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction2()");
System.out.println("Value of ACTION_NAME " + request.getParameter(ActionRequest.ACTION_NAME));
response.setRenderParameter("action", "action2");
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction2()");
}

@ProcessAction(name="action3")
public void handleAction3(ActionRequest request, ActionResponse response)throws PortletException,IOException{
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction3()");
System.out.println("Value of ACTION_NAME " + request.getParameter(ActionRequest.ACTION_NAME));
response.setRenderParameter("action", "action3");
System.out.println("Entering ProcessActionAnnotationPortlet.handleAction3()");
}

}


As you can see i have three methods that are marked with @ProcessAction annotation, the handleAction1, is marked with @ProcessAction(name="action1"), which means that whenever there is processAction call with value of javax.portlet.action parameter equal to action1, it will call handleAction1 method.

The Annotations are implemented using GenericPortlet For a received action the processAction method in the GenericPortlet class tries to dispatch to methods annotated with the tag @ProcessAction(name=<action name>), where the action name must be set on the ActionURL as value of the parameter javax.portlet.action (or via the constant ActionRequest.ACTION_NAME), and following signature:

void <methodname> (ActionRequest, ActionResponse) throws
PortletException, java.io.IOException;

A portlet that wants to leverage this action dispatching needs to set the parameter ActionRequest.ACTION_NAME on the action URL.


This is how my JSP looks like

<%@page language="java" contentType="text/html; %>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
Current Action - <%=renderRequest.getParameter("action") %> <br/>
<portlet:actionURL var="action1Url">
<portlet:param name="javax.portlet.action" value="action1" />
</portlet:actionURL>
<a href='<%=action1Url %>' >Action1 URl</a><br/>

<portlet:actionURL var="action2Url">
<portlet:param name="javax.portlet.action" value="action2" />
</portlet:actionURL>
<a href='<%=action2Url %>' >Action2 URl</a><br/>

<portlet:actionURL var="action3Url">
<portlet:param name="javax.portlet.action" value="action3" />
</portlet:actionURL>
<a href='<%=action3Url %>' >Action3 URl</a><br/>

3 comments:

Anirban Roy Choudhury said...

Will this work the same way for inter portlet communication as well?

I'm having problem to invoke an annotated processAction() using IPC call

joshua said...

Very Good!! thanks

Abhi said...

Thanks for info....
SEO Company in Bangalore