Getting started with Liferay portlet with Java

Wikis > Getting started with Liferay portlet with Java
    Explained the inter communication between jsps and java class

Note: Refer comments for attribute,object,tags explanations.
Lets start with creating a simple portlet using LifeRay.

Portlet is a Java Web-Application that can be embedded into the pages that we have created using Liferay Web Admin. A Liferay page is collection of well arranged portlets.

portal LF

1. Setup LifeRay Eclipse Development evn first.Follow Getting started with Liferay portlet with Java

Below Project structure will be created after finishing with Setting Dev env.
GS1

2.Add a new Portlet through Eclipse > New > LifeRay Portlet Wizard.

GS2

Give the class name and extend it from MVCPortlet class and finish.
GS3

3. Finishing with the above will register the new portlet entries in portel.xml,liferay-portlet.xml and liferay-display.xml as well as it will create a view.jsp {only view mode is selected while adding new portlet} and a class named MyPortlet1.java



4. Now lets add some code in jsp and java to explain the communication between each other.

Already we have created a portlet class. Now lets make some inputs in portlet view jsp.

GS4

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>




Get Age by Name






View jsp will call the getAge() method of MyPortlet1 class as name attribute of actionURL is linked to method name defination in servlet.

Lets put some code in MyPortlet1 so as to explain how the request object is captured ,used and render response is dispatched back to the view end.

package com.test;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class MyPortlet1
 * 
 * @author Vaibs
 *
 */
public class MyPortlet1 extends MVCPortlet {

	public void getAge(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		try {
			String myAge = "Not Found";
			
			//ParamUtil retrieves the values(of primitive data types) from the HttpReqeust. 
			String name = ParamUtil.getString(actionRequest, "pName");
			if (name.equals("Vaibs")) {
				myAge = "26";
				
				//SessionMessages can be used to pass success/failure to the view.
				SessionMessages.add(actionRequest.getPortletSession(),
						"Issue-form-success");
			} else if (name == "Sam") {
				myAge = "28";
				SessionMessages.add(actionRequest.getPortletSession(),
						"Issue-form-success");
			} else {
				SessionMessages.add(actionRequest.getPortletSession(),
						"Issue-form-NotFound");
			}

			// Dispatch the response data to showName.jsp
			actionResponse.setRenderParameter("myAge", myAge);
			actionResponse.setRenderParameter("mvcPath",
					"/html/myportlet1/showAge.jsp");

		} catch (Exception e) {
			SessionErrors.add(actionRequest.getPortletSession(),
					"Issue-form-error");
			e.printStackTrace();

		}

	}

}

Above sample code will get the input value from portlet view and it will dispatch the response after processing it.

showName.jsp

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>





Back

<% String myAgeRender = renderRequest.getParameter("myAge"); %> <% if(SessionMessages.contains(renderRequest.getPortletSession(),"Issue-form-success")){%> Age is :<%=myAgeRender%> <%} else { %> <%} %> <% if(SessionErrors.contains(renderRequest.getPortletSession(),"Issue-form-error")){%> <%} %>

Deploy the portlet or redeploy is already added.
GS5

Go to the add portlet after signing in and drag and drop the MyPortlet1 portlet.

GS6GS7GS8GS9

GettingStartedWithPortletJavaJsp Download