Pages

Thursday, November 10, 2011

Creating a Sample Portlet

Share it Please

In This article we will see how to create a sample Portlet and deploy that in the Portal server. Eclipse IDE will be used for creating the sample portlet and Liferay is used as a portlet framework.
1. Download the necessary software
For this sample application, we need to download the eclipse with LifeRay, liferay SDK and liferay runtime environment. All these can be downloaded from liferay web site. We use tomcat for the liferay runtime environment which can be downloaded from liferay website. Even though we use tomcat as a runtime environment we will deploy the sample portlet in JBoss.
2. Start creating the sample Portlet
Open Eclipse and select a New Liferay Project

Enter the project Name as “HelloWorld”

Enter the Display Name as “HelloWorld”

Select the Location for the Liferay Plug-in SDK.

Select the Location for the Liferay Portal Runtime (this is the unzipped tomcat that we downloaded) and select “Next”

In the next page, select LifeRay MVC as a portlet framework.
Under the Additional Options, check the “Create Custom Portlet class”
Click Next.

In the Next page, select the Portlet class Name “HelloSample”, Java package as “com.test” and Super class as “javax.portlet.GenericPortlet”
Click Next.

In the next page, click next with the default values given.

In the Next page, accept the default values, Under the Liferay Display we can select the Category, we take the default one “Sample”.

Click “Next” and “Finish”

Once the project is created in eclipse, we will see the following directory structure





























These are all the default’s provided by the Liferay when we create a project. There are some important files that we need to know before creating a sample portlet.

liferay-plugin-package.properties

This file describes the plugin to Life ray’s hot deployer.One of the things that can be configured in this file is dependency .jars. If a portlet plugin has dependencies on particular .jar files that already come with Liferay, you can specify them in this file and the hot deployer will modify the .war file on deployment so that those jars are on the class path.

liferay-display.xml
This Page Describes the Category in Which the portal should display under the Add menu

liferay-portlet.xml

This file describes some optional Liferay-specific enhancements for JSR-286 portlets which user can configure when that is installed on a Liferay Portal server. For example, you can set whether a portlet is instanceable or not, which means that you can place more than one instance on a page and each portlet will have its own separate data.
portlet.xml

The portlet deployment descriptor provides the portal server with information about the portlet resources in the application, including configuration, support characteristics, and localized titles. The portal server uses this information to provide services for the portlet.
web.xml

As described in the Java Portlet Specification, a portlet application is an extended web application and therefore has a servlet context. The portlet context leverages most of its functionality from the servlet context of the portlet application. Since the web.xml is a mandatory item in a J2EE web archive, you should make sure your portlet application includes a web.xml in its WAR file that conforms to Version 2.3 of the Java Servlet Specification. This ensures that the portlet application is compatible with other portal server implementations. The web.xml should contain, at a minimum, the <web-app/>, <display-name/>, and <tag-lib/> elements. You can also include context-wide parameters using the <init-param> element.
The <tag-lib/> must indicate the location of the JSR 168 JSP tag library on the portal server, as follows:

     <taglib id="PortletTLD">
        <taglib-uri>http://java.sun.com/portlet</taglib-uri>
        <taglib-location>tld/std-portlet.tld</taglib-location>
     </taglib>

Note: If missing, the <taglib/> element is automatically inserted in the web.xml when a JSR 168 portlet application WAR file is deployed using portal administration or the XML configuration interface. However, the web.xml for JSR 168 EAR files that are installed using predeployment mode are not updated, so including the <taglib/> element is required.

Now let’s write our code in helloSample.java class. I will keep the default changing only the doView() method with this,
 public void doView( RenderRequest renderRequest, RenderResponse renderResponse)
       throws IOException, PortletException {

       renderResponse.setContentType("text/html");
       PrintWriter out=renderResponse.getWriter();
       out.println("Welcome To HelloWorld Sample");
      
    }

The rest of the class looks as it is. I just wrote a println Statement.
By this the creation of sample portlet is done. Just export into a war file.
3. Deploy Sample portlet
Start your JBoss server which has the liferay portal server. Login in as test@liferay.com with password test.
Click “Add” and select “More”. We will see a Dialog Box with more plugins.Click “Install More Applications”.
In the Plugin Installer page, select the “Upload File”. Browse the war file that we generated and select to install.
Once installed, select “Back To Liferay.com” and click “Add” once again ,Enter “helloSample” in the text field. we will see a list of plugins that are installed. Click Add beside the “HelloSample” plugin.
We will see the HelloSample on the Liferay main page.
By this we developed a sample portlet and deployed in the Liferay Portal Server.
4. Basic Issues
When I tried to install the sample portlet for the first time ,it failed giving me this error
ERROR [STDERR] Failed to copy /tmp/20111020102225774/WEB-INF/classes/logging.properties to /usr/liferay-portal-6.0.6/jboss-5.1.0/server/default/deploy/HelloSample-portlet.war/WEB-INF/classes/logging.properties due to failed to create the parent directory for /usr/liferay-portal-6.0.6/jboss-5.1.0/server/default/deploy/HelloSample-portlet.war/WEB-INF/classes/logging.properties

I had to create a directory called “content” with a properties file “language.properties”
More Articles To Come, Happy Coding.............
 


No comments :

Post a Comment