Building portlet applications
More in Ebooks
Download:http://rapidshare.com/files/256024670/Building.portlet.Application.rar
Read more...
More in Ebooks
Download:http://rapidshare.com/files/256024670/Building.portlet.Application.rar
The service builder framework in Liferay represents the database layer and all the interactions with database are done through service builder infrastructure. So in this blog, I will explain how you can use service builder framework inside your custom portlet using NetBeans 6.7 & Portal Pack 3.0. To use service builder framework, you first need to create a service xml and then generate the required code. The Portal Pack here helps you by providing a nice GUI editor for service.xml file where you can define the entities or database structures and from the same GUI you can generate the services code which can be used inside your portlet.
I have taken an example of Simple Form Submission Portlet here. Using this portlet you can submit customer details which will be stored inside liferay's database and the same portlet will also show the no of customer records present in the database.
First create a Webapplication with "Portlet Support" framework and add a new Portlet to it. Now you need to create the service.xml file. Let's create the portlet application as "CustomerApp" and a portlet "CustomerPortlet" inside it.
After creating a portlet application, you need to create a service.xml file inside the portlet application.
From the CustomerApp application, right click on Web Pages and select New > Others > Web Space/Liferay Plugins > Service Builder XML
Enter the file name as service. Make sure the "Folder" is selected as web. Then click Finish.
The service.xml file is created inside the web directory and the IDE opens the service.xml inside the editor area.
You can see two tabs "Design" and "XML" in the editor. The "Design" tab helps the user to add/modify entities through GUI and using "XML", user can directly modify the xml.
Now we are ready to add entities to our service xml.
Click on "Add" button.
Specify the Entity Name as "CustomerDtls" and table name as "CustomerDtls". The Entity name and table name doesn't need to be same.
Change the default Package Path name to com.sample.customer and namespace to "customer". The namespace should be unique for every portlet application. No two portlet applications should have the same namespace.
After creating an entity, you need to add columns for that entity. So to create columns for an entity
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.portlet.*"%>
<%@ page import="com.example.customer.service.*"%>
<%@ page import="com.example.customer.model.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<%PortletPreferences prefs = renderRequest.getPreferences();%>
<table>
<form method="POST" action="<portlet:actionURL/>">
<tr>
<td>Customer ID:</td>
<td><Input type="text" name="id"/></td>
</tr>
<tr>
<td>Name:</td>
<td><Input type="text" name="name"/></td>
</tr>
<tr>
<td>Age:</td>
<td><Input type="text" name="age"/></td>
</tr>
<tr>
<td><Input type="submit"/></td>
</tr>
</form>
</table>
<H3> No of Customers in Database :
<%
out.println(CustomerDtlsLocalServiceUtil.getCustomerDtlsesCount());
%>
</H3>
public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
try {
long id = Long.parseLong(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
CustomerDtls customer = CustomerDtlsLocalServiceUtil.createCustomerDtls(id);
customer.setId(id);
customer.setName(name);
customer.setAge(age);
CustomerDtlsLocalServiceUtil.addCustomerDtls(customer);
System.out.println("Added");
} catch (Exception ex) {
ex.printStackTrace();
}
}
So we are now ready to deploy the portlet application. Right click on the portlet project and select "Deploy" to deploy the portlet on Liferay Portal Server. I am assuming you have selected "Liferay Portal Server" as runtime for the portlet application. The required table is created automatically during the deployment time, so ideally you don't need to create the table manually. But incase you get "Table not Found Exception" while accessing the portlet, you can run the generated table.sql manually to create the required tables. The table.sql can be found under "WEB-INF/sql" directory.