RenderMode annotation

The doDispatch method of GenericPortlet class is modified in the Portlet Specification 2.0, so that now when it gets control first it checks if there is any method in your Portlet class which has @RenderMode annotation that matches the portlet mode of incoming request, if yes it forwards control to that method, if not then the doDispatch method will forward control to following methods

  • doView() for handling VIEW mode

  • doEdit() for handling EDIT mode

  • doHelp() for handling HELP mode



This is how the doDispatch() method in GenericPortlet class looks like

protected void doDispatch(RenderRequest request, RenderResponse response) throws PortletException,
java.io.IOException {
WindowState state = request.getWindowState();
if (!state.equals(WindowState.MINIMIZED)) {
PortletMode mode = request.getPortletMode();
// first look if there are methods annotated for
// handling the rendering of this mode
try {
// check if mode is cached
Method renderMethod = renderModeHandlingMethodsMap.get(mode.toString());
if (renderMethod != null) {
renderMethod.invoke(this, request, response);
return;
}
} catch (Exception e) {
throw new PortletException(e);
}

// if not, try the default doXYZ methods
if (mode.equals(PortletMode.VIEW)) {
doView(request, response);
} else if (mode.equals(PortletMode.EDIT)) {
doEdit(request, response);
} else if (mode.equals(PortletMode.HELP)) {
doHelp(request, response);
} else {
throw new PortletException("unknown portlet mode: " + mode);
}
}


I wanted to see how @RenderMode annotation works so i built a sample portlet like this, you can download it from here


package com.webspherenotes.portlet.jsr286;

import java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderMode;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class RenderModeAnnotationPortlet extends GenericPortlet{

@RenderMode(name="view")
public void handleViewMode(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ProcessActionAnnotationPortlet.handleViewMode()");
response.setContentType("text/html");
response.getWriter().println("<h3>View Mode Response</h3>");
System.out.println("Exiting ProcessActionAnnotationPortlet.handleViewMode()");
}

@RenderMode(name="edit")
public void handleEditMode(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ProcessActionAnnotationPortlet.handleEditMode()");
response.setContentType("text/html");
response.getWriter().println("<h3>Edit Mode Response</h3>");
System.out.println("Exiting ProcessActionAnnotationPortlet.handleEditMode()");
}

@RenderMode(name="help")
public void handleHelpMode(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ProcessActionAnnotationPortlet.handleHelpMode()");
response.setContentType("text/html");
response.getWriter().println("<h3>Help Mode Response</h3>");
System.out.println("Exiting ProcessActionAnnotationPortlet.handleHelpMode()");
}
}


If you want to use @RenderMode annotation, then you will have to do two things, first create a method with following signature


void (RenderRequest, RenderResponse) throws
PortletException, java.io.IOException;


Mark that method with @RenderMode(name="<modename>") attribute where value of name equals to name of the PortletMode that this method should handle

2 comments:

Björn said...

Thanks for a good article.

Is it possible to use annotated if you have the action url in a form with a submit button instead of as a action link?

I have tried this without success. Preferably I would like to have the possibilit to have several submit buttons within a form, where each button is related with its own annotated process action method.

Abhi said...

Thanks for info....
SEO Company in Bangalore