Monday, February 7, 2011

Create Portlet with different modes (edit, view, help). In Liferay6.0

Create Portlet with different modes (edit, view, help). In Liferay6.0
(Should work with liferay 5.2.3)
  1. Install and setup Portal code 6.0.
  2. Install Plugin code 6.0
  3. Go to command prompt >workspace>pluginsdk>portlet> create.bat hello-world "Hello World"
  4. Go to portlet.xml under WEB-INF and update
  5. <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
To
<portlet-class>com.liferayinaction.portlet.HelloYouPortlet</portlet-class>
  1. Add the edit mode along with view mode.
<init-param>
<name>edit-jsp</name>
<value>/edit.jsp</value>
</init-param>
  1. Under WEB-INF create a src folder and add the class
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class HelloYouPortlet extends GenericPortlet {
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
viewJSP = getInitParameter("view-jsp");
}

public void doEdit(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
renderResponse.setContentType("text/html");

PortletURL addName = renderResponse.createActionURL();
addName.setParameter("addName", "addName");
renderRequest.setAttribute("addNameUrl", addName.toString());
include(editJSP, renderRequest, renderResponse);
}

public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
PortletPreferences prefs = renderRequest.getPreferences();
String username = (String) prefs.getValue("name", "no");
if (username.equalsIgnoreCase("no")) {
username = "";
}
renderRequest.setAttribute("userName", username);
include(viewJSP, renderRequest, renderResponse);
}

public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String addName = actionRequest.getParameter("addName");
if (addName != null) {
PortletPreferences prefs = actionRequest.getPreferences();
prefs.setValue("name", actionRequest.getParameter("username"));
prefs.store();
actionResponse.setPortletMode(PortletMode.VIEW);
}
}

protected void include(String path, RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher = getPortletContext()
.getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
} else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String editJSP;
protected String viewJSP;
private static Log _log = LogFactory.getLog(HelloYouPortlet.class);
}
  1. Update view.jsp as
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean id="userName" class="java.lang.String" scope="request"/>
<portlet:defineObjects />
<p>This is the Hello You portlet.</p>
<p>Hello <%=userName %>!</p>
  1. Create edit.jsp at same location as view.jsp
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean class="java.lang.String" id="addNameUrl" scope="request" />
<portlet:defineObjects />
<form
id = "<portlet:namespace />helloForm"
action="<%=addNameUrl %>"
method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="username"></td>
</tr>
</table>
<input type="submit" id="nameButton" title="Add Name" value="Add Name">
  1. Add the following to portlet.xml
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>

  1. From ant-build.xml compile and deploy.
  2. Start Tomcat and Liferay. Now on view there will be hello displayed.
  3. Click on the button of the portlet where there is look and feel
Go to preference and type in Harshal.
  1. Click on Add which will take back to view now the portlet will display hello harshal.

No comments:

Post a Comment