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

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

srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store