Create a New Liferay project
Right click on the project àNew àLiferay Service Builder àafter selecting this Enter package path(Ex:com.karthik),Namespace(Ex:B)
and author(Ex:karthik)àInclude sample entity in new file(Enable
this by default it is enable keep as it is)àFinish
service.xml
change your code with below code
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE service-builder
PUBLIC "-//Liferay//DTD
Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.karthik">
<author>karthik</author>
<namespace>B</namespace>
<entity name="Employee"
local-service="true" remote-service="false">
<!-- PK fields
-->
<column name="empId"
type="long" primary="true"
/>
<column name="fname"
type="String" />
<column name="lname"
type="String" />
<column name="address"
type="String" />
<column name="email"
type="String" />
</entity>
</service-builder>
Select Overview(left corner) and click build services
After this create a portlet and jsp’s to insert the data and
display output in browser
view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<portlet:defineObjects />
<portlet:actionURL var="employeeURL"
name="insert"></portlet:actionURL>
<aui:form action="<%=employeeURL%>"
method="post" name="insert"
id="insert">
<table>
<tr>
<td>First Name:</td>
<td><input type="text"
name="fname" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text"
name=lname "
/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text"
name="address" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text"
name="email" /></td>
</tr>
<tr>
<td colspan=2><aui:button
type="submit" value="Submit"></aui:button></td>
</tr>
</table>
</aui:form>
EmpPortlet.java
package com.karthik;
import java.io.IOException;
import
javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
javax.portlet.PortletException;
import com.karthik.model.Employee;
import
com.karthik.service.EmployeeLocalServiceUtil;
import
com.liferay.portal.kernel.util.ParamUtil;
import
com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class
EmpPortlet
*/
public class EmpPortlet extends MVCPortlet
{
public void insert(ActionRequest
actionRequest,
ActionResponse
actionResponse) throws IOException, PortletException
{
try
{
String fname = ParamUtil.getString(actionRequest,"fname");
String lname = ParamUtil.getString(actionRequest,"lname");
String address = ParamUtil.getString(actionRequest,"address");
String email = ParamUtil.getString(actionRequest,"email");
Employee employee=EmployeeLocalServiceUtil.insertEmployee(fname,lname,address,email);
long empId=employee.getEmpId();
actionRequest.setAttribute("empId", empId);
actionRequest.setAttribute("fname", fname);
actionRequest.setAttribute("lname", lname);
actionRequest.setAttribute("address",address);
actionRequest.setAttribute("email",email);
actionResponse.setRenderParameter("jspPage",
"/jsp/emp/success.jsp");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Change the below code in
com.karthik.service.implàEmployeeLocalServiceImpl.java
package
com.karthik.service.impl;
import
com.karthik.model.Employee;
import
com.karthik.service.base.EmployeeLocalServiceBaseImpl;
import
com.liferay.portal.kernel.exception.SystemException;
public class
EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl
{
public Employee
insertEmployee(String fname, String lname,
String
address,String email)
{
try
{
long empId = counterLocalService.increment(Employee.class
.getName());
Employee employee = employeePersistence.create(empId);
employee.setFname(fname);
employee.setLname(lname);
employee.setAddress(address);
employee.setEmail(email);
return employeePersistence.update(employee,false);
}
catch (SystemException e)
{
e.printStackTrace();
}
return null;
}
}
success.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%=renderRequest.getAttribute("fname") %></br>
<%=renderRequest.getAttribute("lname") %></br>
<%=renderRequest.getAttribute("address") %></br>
<%=renderRequest.getAttribute("email") %>
Note:If you get any compile errors build once again after adding the code in portlet and LocalServiceImpl