Monday, April 28, 2014

Generic Portlet Development with Liferay Plugins SDK and Liferay IDE

Introduction:

Liferay Community has developed Liferay IDE to develop portlets in Liferay environment. We will use Liferay Plugins SDK with Liferay IDE to develop portlet and deploy into liferay portal servers.

Liferay IDE is eclipse Plugin so that it will provide liferay application development environments i.e. portlets, hook, layout and themes development. It provides a GUI based features to create Liferay Applications.

In the previous article we have seen develop Generic Portlet with Liferay Plugins SDK and ANT tool.


Now we will develop same Generic Portlet with Liferay Plugins SDK and Liferay IDE.

Prerequisite Step:


Once we done all steps in above article then we are ready with Liferay Development Environment with Liferay IDE.

Now we have to choose Liferay Prospective in the Eclipse.

The following is Liferay Prospective in Eclipse



Creating Generic Portlet with Liferay IDE

Step: 1

In the eclipse click on create new liferay project option in eclipse.

The following is screen show create new liferay project


Step: 2

In Project Dialog we need to enter project name and portlet display name.
We need select Plugin Type portlet because we are developing portlet and click on next button.

The following screen show Project Dialog


Step: 3

Once we did above steps now we need select Liferay MVC radio button and need to click on select custom portlet class.

When we select this then we can use our own portlet class and that will be configured in portlet.xml file



Step: 4

Now we need to provide our custom class name and need specify the package name.
Here important thing is we are going develop Generic Portlet so we need to extend GenericPortlet.java class to our custom portlet.



Step: 5

Once we did above step one more dialog will as some other information Like Portlet Name, Display Name and where jsp page will be places like path of JSPs and now click on next. This information will be used in portlet.xml, liferay-portlet.xml


Step: 6

Now we need to specify the portlet category information and css and js files information it have default configuration we can keep same then portlet will be fall under sample category. This information will be used in liferay-display.xml file.



Once we done all above steps final step is we need to click on finish button. As soon as we click on finish button then portlet project will be create and default configuration files and project directory structure will be created.

In the Package explorer you can see the project directory structure in left side area of eclipse and you can see portlet related ant file in ANT view at the right side area.



Now we have successfully created Generic Portlet project and it will have basic configuration files along with the directory structure. Now we can deploy the project and we can use the generic portlet.

Portlet Deployment:

When we create portlet project then each project has ANT build file i.e. build.xml file, it has ant targets to deploy the portlets in liferay portal servers.

In the eclipse if we see in ANT view right side, you can see portlet project build file. If you e open tree view then you can see all available ANT targets for the portlet project.



In the available targets one should be default one that is you can see different color in the ANT view.

We will use ant deploy target to deploy the portlet in liferay portal server.

Simply double click on deploy target in the eclipse ANT view so that portlet will be deployed into liferay portal server that is tomcat or Jboss

When we run deploy target all log message you can see in eclipse console view.


If no compile time error then it will show build success message otherwise it show compiles time errors in the console.

If build success then it will be packages as war file and deployed into liferay deploy directory.

As soon as war file places in liferay deploy directory auto deploy scanner will copy the portlet into server deployment directory and it will be extracted in deployment directory then portlet available in the application so that we use it.

Start Liferay portal Server

Before you deploy the portlet your liferay portal server should be start mode.

We can also start server from eclipse that is configures servers in servers views you can simple click on start option then server will be start.




Once we successfully deployed portlet and start the server then portlet available to add in page.

Using Portlet:

Access Liferay portal using you host name port

http://localhost:8080

Generally when create portlet then it will be available in sample category default. We already know at time of creation we will provide the category we can find there.

Login as liferay admin in liferay portal in the application section you can see the sample category.

The following is login screen




Add portlet to page




Now if we want developed code or modify/add jsps we can do it for project and need to deploy again so that changes will be applied portlet.

Important Points
  • When we create project from eclipse it will create all basic configuration files and required directory structure for the portlet project.
  • Configuration files are like portlet.xml, liferay-portlet.xml, liferay-disply.xml file and web.xml and all these are available in WEB-INF directory.
  • Portlet class is available in src directory we can modify.
  • Jsp pages are available in the path we specified in project creation so that we can add new jsp pages or we can modify existed jsp pages that are default jsp page.
  • As we already know portlet class should extends Generic Portlet class so that we can call it as Generic Portlet.
  • When we run ant deploy target first all java classes will be compiled and packages as war file then placed in liferay deploy directory.
  • As soon as it will be placed in deploy directory then auto deploy scanner will add additional configurations to the portlet and placed into server deployment directory then portlet available

More Details about Generic Portlet Please go through following Articles



Note:

Generic Portlet is very basic and legacy portlet generally in real time we will use Liferay MVC portlet, JSF, Struts Portlet or Spring Portlet

Portlet Anatomy/Directory structure

The following is portlet directory structure



  • In Main Project parent directory we have docroot and build.xml file available.
  • Build.xml file for build the liferay portlet and it is any file so that we can deploy the portlet.
  • In Liferay all files and directories will be places in docroot
  • In side docroot we have WEB-INF and this will contains all xml files, clasess and lib directory and also src directory for java classes.
  • Jsp pages will be available in direct docroot.
  •  docroot is directory just for development conveniences after packaged as war file docroot will be excluded.
  • You can add more code samples which I have given in previous articles and you can download from my source forge location.

Download Generic Portlet Examples


Employee Generic Portlet Code samples

Web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
       <display-name>EmployeeGeneric-portlet</display-name>
      
       <jsp-config>
              <taglib>
                     <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
                     <taglib-location>
                           /WEB-INF/tld/liferay-portlet.tld
                     </taglib-location>
              </taglib>
              <taglib>
                     <taglib-uri>http://liferay.com/tld/aui</taglib-uri>
                     <taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
              </taglib>
       </jsp-config>
</web-app>


Portlet.xml


<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
 http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
       <portlet>
              <portlet-name>employeeportlet</portlet-name>
              <display-name>Employee Generic Portlet</display-name>
              <portlet-class>
                     com.meera.genericportlet.EmployeePortletAction
              </portlet-class>
              <init-param>
                     <name>view-template</name>
                     <value>/html/jsps/view.jsp</value>
              </init-param>
              <expiration-cache>0</expiration-cache>
              <supports>
                     <mime-type>text/html</mime-type>
                     <portlet-mode>view</portlet-mode>
              </supports>
              <portlet-info>
                     <title>Employee Generic Portlet</title>
                     <short-title>Employee Portlet Action</short-title>
                     <keywords></keywords>
              </portlet-info>
              <security-role-ref>
                     <role-name>administrator</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>guest</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>power-user</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>user</role-name>
              </security-role-ref>
       </portlet>
</portlet-app>


Liferay-portlet.xml


<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN"
 "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
       <portlet>
              <portlet-name>employeeportlet</portlet-name>
              <icon>/icon.png</icon>
              <instanceable>false</instanceable>
              <header-portlet-css>/css/main.css</header-portlet-css>
              <footer-portlet-javascript>
                     /js/main.js
              </footer-portlet-javascript>
              <css-class-wrapper>employeeportlet-portlet</css-class-wrapper>
       </portlet>
       <role-mapper>
              <role-name>administrator</role-name>
              <role-link>Administrator</role-link>
       </role-mapper>
       <role-mapper>
              <role-name>guest</role-name>
              <role-link>Guest</role-link>
       </role-mapper>
       <role-mapper>
              <role-name>power-user</role-name>
              <role-link>Power User</role-link>
       </role-mapper>
       <role-mapper>
              <role-name>user</role-name>
              <role-link>User</role-link>
       </role-mapper>
</liferay-portlet-app>


Liferay-display.xml


<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.2.0//EN"
 "http://www.liferay.com/dtd/liferay-display_6_2_0.dtd">
<display>
      
       <category name="category.sample">
              <portlet id="employeeportlet"></portlet>
       </category>
</display>


Portlet Action Class(EmployeePortletAction.java)

package com.meera.genericportlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

public class EmployeePortletAction extends GenericPortlet {

       public void init() {
        viewJSP = getInitParameter("view-template");
    }
   
    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {
       String currentViewPage=renderRequest.getParameter("jspPage");
       if(currentViewPage!=null&&!currentViewPage.equals("")){
              include(currentViewPage, renderRequest, renderResponse);
       }else{
              include(viewJSP, renderRequest, renderResponse);
       }
    }
   
     public void processAction(ActionRequest request,ActionResponse response)throws PortletException,java.io.IOException{
        String emplyeeName=request.getParameter("employeeName");
        String employeeAddress=request.getParameter("employeeAddress");
        Map<String,String> employeeMap=new HashMap<String,String>();
        employeeMap.put("employeeName",emplyeeName);
        employeeMap.put("employeeAddress",employeeAddress);
        request.setAttribute("employeeMap", employeeMap);
        response.setRenderParameter("jspPage","/html/jsps/displayEmployees.jsp");
    }

    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 viewJSP;


    private static Log _log = LogFactoryUtil.getLog(EmployeePortletAction.class);

}


Default portlet view page i.e. view.jsp (/html/jsps/view.jsp)


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:renderURL  var="addEmployee" windowState="normal">
<portlet:param name="jspPage" value="/html/jsps/addEmployee.jsp"/>
</portlet:renderURL>
<portlet:renderURL  var="displayEmployees" windowState="normal">
<portlet:param name="jspPage" value="/html/jsps/displayEmployees.jsp"/>
<portlet:param name="emplyeeName" value="Meera Prince"/>
</portlet:renderURL>
<h1>Welcome to  Employee Generic Portlet</h1>
<a href="<%=addEmployee.toString()%>">Add Employee</a><br/>
<a href="<%=displayEmployees.toString()%>">View All Employees</a><br/>


Add employee jsp page (/html/jsps/addEmployee.jsp)


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL var="addEmployeeActionURL" windowState="normal">
</portlet:actionURL>
<h1>This JSP Page for Add Employee</h1>
<form action="<%=addEmployeeActionURL%>" name="emplyeeForm"  method="POST">
Employee Name<br/>
<input  type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/><br/>
Employee Address<br/>
<input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/><br/>
<input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/>
</form>



Display employee jsp page (/html/jsps/displayEmployees.jsp)


<%@page import="java.util.Map"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<h1>Display Employee Details</h1>
<%
Map<String,String> employeeMap=(Map<String,String>)renderRequest.getAttribute("employeeMap");
if(employeeMap!=null){
%>
Emplyee Name: <%=employeeMap.get("employeeName")%>     <br/>
Emplyee Address: <%=employeeMap.get("employeeAddress")%><b/>
<%}%>


The following are portlet application screens

Default portlet screen


Add employee screen


Display Employee Details Screen


Create Liferay Portlet Project form existing source with Liferay IDE

Before we have seen creating New Portlet Project with Liferay IDE and Eclipse now we will see create liferay portlet from existing source.

Generally we can download many portlet examples from different places we want use it in our development we need create project from existed source code.

Step: 1

We need place the downloaded portlet or source portlet in Liferay Plugins SDK portlets directory.


Example:

D:\Liferay Work Space\liferay-plugins-sdk-6.2.0\portlets


Step: 2

Now open project directory and delete all environment related configuration files like .classpath, .project, .settings.

These files are related to previous developed environment configuration so we don’t need these files so simply delete.


Step: 3

Go to eclipse and click on create Liferay project there we need select create new liferay project from existing source code option.




Step: 4

Now browse project location which in Liferay Plugins SDK portlets directory.



Once we select the project then click on finish button then portlet project will be created from existing source code and that will be visible in package explorer right side.

Step: 5

Now we need to add ant build file to ANT vies so that we can run ant commands.

Simply open project in package explorer and find ant build file then drag and drop build.xml in ANT view that is in right side



Now we can run ant targets by simply double click on targets in ANT view.

If you have any environmental Challenges go through following link


Note:

Liferay Plugins SDK with Liferay IDE is good for portlet development and we will use other portlets in real time rather than Generic Portlet but we need to have more understanding about Generic Portlet then we will know more in future development.

Author

0 comments :

Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts