Saturday, 1 November 2014

Portlet Development Beginners Concept Building

Portlet Development Beginners Concept Building



To start development of portlets, let us inspect how things work. 

When an user lands on a portal page, the portlets in that page shows their content after going through render methods like doView(). 
Suppose there is a form present in a portlet and the user puts some data and submits the form. Then that portlet goes through a two phase lifecycle.
First the processAction() method is executed and then the render method like doView() gets executed.

In the processAction() method, we typically do POST actions, in the sense that we retrieve form data, do some processing, connect to a database and fetch data and perform other business logics. 

In the render methods (doView, doHelp, doEdit) we typically do GET actions like getting different objects from the application/session etc and setting the data in request/session as per requirement of the corresponding jsp page that is to be displayed to the user. 

Also the communication between the processAction() method and the render methods of the portlet is achieved through the setRenderParam methods of the ActionResponse object in the processAction method.

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
String userData = request.getParameter("formParam");
response.setRenderParameter("userParam", userData);
}

The parameter set in the response object above are accessible in the doView() method as shown below:

public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType(request.getResponseContentType());
System.out.println("Printing from doView() method:"+request.getParameter("userParam"));
PortletRequestDispatcher requestDispatcher = getPortletContext().getRequestDispatcher(getJspFilePath(request, "myHomeView.jsp));
requestDispatcher.include(request,response);
}


In jsp, we can get some implicit portlet objects like portletConfig, portletSession, renderResponse, renderRequest etc. But to make these implicit objects  available we need to write the following tag in the jsp:
<portlet:defineObjects/>

portletConfig is an object of the type PortletConfig
portletSession is an object of the type PortletSession
renderRequest is an object of the type RenderRequest
renderResponse is an object of the type RenderResponse

In this fashion, any parameter set in the processAction() method can be accessible from the jsp as shown below:
<% renderRequest.getParameter(“userParam”); %>


No comments:

Post a Comment