Liferay Portlet Development
Liferay Portlet Development
Table of Contents
Preface.................................................................................................................................................. 4 Evolution of this book......................................................................................................................4 Who should read this book?.............................................................................................................4 What is not covered in this book?....................................................................................................4 Conventions..................................................................................................................................... 4 About the authors.............................................................................................................................4 Updates............................................................................................................................................ 4 Publisher's Note............................................................................................................................... 4 System Requirements...................................................................................................................... 4 1. Introduction...................................................................................................................................... 4 Definition of a Portal....................................................................................................................... 4 What are portlets?............................................................................................................................ 4 Portlet Specification (JSR286).........................................................................................................4 Why Liferay and what makes it special?......................................................................................... 4 2. Setup................................................................................................................................................. 4 Prerequisites for getting Liferay up and running............................................................................. 4 a. Installing JDK......................................................................................................................... 5 b. Installing MySQL................................................................................................................... 5 c. Installing Ant........................................................................................................................... 5 Getting Liferay up and running....................................................................................................... 5 a. Unzip the liferay tomcat bundle.............................................................................................. 5 b. Remove all default plugins..................................................................................................... 5 c. Make Liferay work with a production ready database............................................................ 5 d. Starting up the Liferay Server................................................................................................. 5 Liferay Plugins SDK........................................................................................................................6 a. Installation............................................................................................................................... 6 b. Configuration.......................................................................................................................... 6 c. Create simple portlets..............................................................................................................6 Development Tools.......................................................................................................................... 6 a. Installing the latest version of Eclipse IDE............................................................................. 6 b. Installing Liferay Eclipse IDE................................................................................................ 6 c. Creating a simple portlet using Liferay IDE........................................................................... 6 d. Anatomy of a portlet............................................................................................................... 6 Supporting Tools.............................................................................................................................. 6 a. MySQL Query Browser.......................................................................................................... 6 b. Mozilla Firefox and Firebug................................................................................................... 6 c. An Unzip utility WinRAR / WinZip.....................................................................................6 3. Library Management System........................................................................................................... 6 Create a new Liferay Plug-in Project...............................................................................................6 Deploying library-portlet to the server.........................................................................................9 Adding the portlet to a page...........................................................................................................10 Some code clean-up before we start.............................................................................................. 11 4. Creating a Form.............................................................................................................................. 11 Establishing a basic page flow....................................................................................................... 11 Creating a form to add book.......................................................................................................... 12 Learnings from this chapter........................................................................................................... 14 Converting Simple HTML form to AUI form............................................................................... 14
Liferay Portlet Development A definitive guide 2
References.......................................................................................................................................... 15
Preface
Evolution of this book Who should read this book? What is not covered in this book? Conventions About the authors Updates Publisher's Note System Requirements
1. Introduction
Definition of a Portal What are portlets? Portlet Specification (JSR286) Why Liferay and what makes it special?
2. Setup
Prerequisites for getting Liferay up and running
Create a prepackaged bundle and store on the cloud
Development Tools a. Installing the latest version of Eclipse IDE b. Installing Liferay Eclipse IDE c. Creating a simple portlet using Liferay IDE d. Anatomy of a portlet
Supporting Tools a. MySQL Query Browser b. Mozilla Firefox and Firebug c. An Unzip utility WinRAR / WinZip
1. give the project name as library (all small letters) 2. make sure the plug-ins SDK and Liferay Portal Runtime are configured properly 3. check Create custom portlet class 4. click Next
1. change the portlet class to LibraryPortlet 2. java package as com.library 3. select superclass of this portlet as com.liferay.util.bridges.mvc.MVCPortlet 4. click Next
1. modify Display name and Title to have a space between the words Library and Portlet 2. check Edit portlet mode 3. modify JSP folder to /html/library 4. check the option Create resource bundle file 5. click Next
1. uncheck Allow multiple instances to make this portlet non-instansable 2. specify a new Category - Library Management 3. click Finish Now you see a new eclipse project with name library-portlet.
In the next dialoy, move library-portlet from left to right and click Finish as shown below.
Observe the server console and confirm that you get the message 1 portlet for library-portlet is available for use.
4. Creating a Form
Establishing a basic page flow
In this chapter, we will see how to create some basic page flows for the library portlet we have just created. Create a new page Add one new JSP file called update.jsp inside docroot/html/library and put some dummy contents and Save this file.
<h1>Add / Edit Form</h1>
Modify view.jsp 1. Open view.jsp 2. remove the line This is the <b>Library Portlet</b> portlet in View mode. 3. enter the code to link to update.jsp 4. check the portlet and you should see a link to update.jsp.
<portlet:renderURL var="updateBookURL">
create init.jsp create a new file init.jsp where all common stuff will be put. This file in turn will be included in all other JSP's of this portlet. This way, we need not have to repeat the same code again and again in all JSP files. Remove these lines from view.jsp and paste into init.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects />
insert this line at the top of all other JSP files we have so far.
<%@ include file="/html/library/init.jsp" %>
The interfaces PortletURL and ActionRequest will report problem. To get rid of them just add the following imports in your init.jsp.
Liferay Portlet Development A definitive guide 12
Inside the JSP scriplet we have programmatically declared a variable updateBookURL which is of type actionURL. We have also set one attribute for this object, the ACTION_NAME. Once you save all files and the portlet gets deployed, check the Add Book page and you will see something like this,
Enter some values and click Add, you will get some error on the page Portlet is temporarily unavailable. Let us check the eclipse console to know what is causing the problem,
It is clear from the message that the portal server is unable to find a method updateBook In the next step let us see how and where to add this method. Modify LibraryPortlet.java
public void updateBook(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String bookTitle = ParamUtil.getString(actionRequest, "bookTitle"); String author = ParamUtil.getString(actionRequest, "author"); } System.out.println("Your inputs ==> " + bookTitle + ", " + author);
Now re-deploy the portlet and check the code in our updateBook method is being called properly. Once you enter the details of a book and submit the form you should get the message on the console,
Your inputs ==> Liferay In Action, Richard Sezov
1. when you use AUI, you need not have to explicitly give <portlet:namespace/> 2. ensure that you have explicitly specified the method attribute for <aui:form> tag. 3. See we have specified the label attribute only for bookTitle and not for author. Why? Once our changes are deployed, you will see a more cleaner / better form,
We will see more applications of Alloy UI (AUI) in subsequent chapters. For a detailed discussion on AUI, please refer to the below Liferay Wiki article, http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Alloy+UI+Forms+(aui)
</header-portlet-javascript>
3. open update.jsp To know more about integrating jQuery with liferay, read the following liferay Wiki articles. http://www.liferay.com/web/jonas.yuan/blog/-/blogs/building-jquery-based-plugins-in-liferay-6 http://www.liferay.com/web/julio.camarero/blog/-/blogs/can-i-have-different-jquery-versions-inliferay http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/using-jquery-or-any-javascript-libraryin-liferay-6-0
Liferay Portlet Development A definitive guide 15
In the next popup, give the properties of the service layer we are going to generate like, package path and Namespace. We recommend the package path to look something like com.library.slayer, where slayer stands for service layer, so that all the generated files go under this package and do not interfere with the files that we create for the portlet.
Once you click Finish, you will see the service.xml file opening as below.
Click the XML tab on this window to see the corresponding XML file.
Now edit the service.xml file by replacing the entity element with the following entity definition.
<entity name="LMSBook" local-service="true" remote-service="false"> <!-- PK fields --> <column name="bookId" type="long" primary="true" /> <!-- UI fields --> <column name="bookTitle" type="String" /> <column name="author" type="String" /> <!-- Audit fields --> <column name="dateAdded" type="Date" /> </entity>
In this most simplistic service.xml file, we have defined one field as primary key, two UI fields and one audit field. We have also specified local-service as true and remote-service as false. Now save the changes and click Build services button. If all well you will see the BUILD SUCCESSFUL message on the console as below. If not, something might be wrong. Please go back and check your service.xml as there could be some syntactical errors.
Now you see the complete list of files that are generated by the service layer.
// insert the book using persistence api try { LMSBookLocalServiceUtil.addLMSBook(book); } catch (SystemException e) { e.printStackTrace(); }
Also make sure that you have made the necessary imports to this java file. Save all your changes and observe that the portlet is getting deployed properly. Go to your form and add a book. Confirm the book information is getting inserted to the database by opening the MySQL Query Browser. You will see a new table LMS_LMSBook. Keeping adding more books through the form and see the records are getting inserted into this table. Congratulations!! You have successfully integrated a service layer api with our application. In the next section we are going to see how to retrieve the records of our table and show in a new page, list.jsp.
4. Go to the browser and check the new link is correctly taking you to the List of books 5. Add the following code to list.jsp to get the list of books and display
<%
%> <table border="1" width="80%"> <tr> <th>Book Title</th> <th>Author</th> <th>Date Added</th> </tr> <% for (LMSBook book : books) { %> <tr> <td><%= book.getBookTitle() %></td> <td><%= book.getAuthor() %></td> <td><%= book.getDateAdded() %></td> </tr> <% }
6. Go to the portlet in browser and click on Show all books, you will see all the books getting listed out as below.
Congratulations!! you have successfully retrieved all the books of our library and displayed them on an new page. We have made use of two new API's of LMSBookLocalServiceUtil getLMSBooksCount() and getLMSBooks(0, count).
form.
<aui:input type="hidden" name="redirectURL" value="<%= renderResponse.createRenderURL().toString() %>"/>
2. Changes to the portlet class Open LibraryPortlet.java and add the following lines to the end of the updateBook method.
// gracefully redirecting to the default portlet view String redirectURL = ParamUtil.getString(actionRequest, "redirectURL"); actionResponse.sendRedirect(redirectURL);
3. Save all your changes and check the refresh problem got suitably addressed by now.