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") %>
hi i am getting an error of protlet not available when i click on submit
ReplyDeletecan u help me out ?
Hi saiyam,
DeleteThe above code is working fine. Build the services again and try. If it is not working post the error here(what you are getting in console).
The success.jsp is not working. it only shows "Your request completed successfully" after i click the add button.
ReplyDeleteHi,
DeleteThe above code is working fine.Please check your jsp folder name and path in the following code
actionResponse.setRenderParameter("jspPage", "/jsp/emp/success.jsp");
or change the above code with your jsp names.
hello, where did u put the method "insertEmployee" actually??? in the EmployeeLocalServiceUtil or in the EmployeeLocalServiceImpl?? how come in the "EmpPortlet.java" file when u called the class EmployeeLocalServiceUtil it can detect the method that was created in the EmployeeLocalServiceImpl??
ReplyDeleteThis comment has been removed by the author.
Deletehello, where did u put the method "insertEmployee" actually??? in the EmployeeLocalServiceUtil or in the EmployeeLocalServiceImpl?? how come in the "EmpPortlet.java" file when u called the class EmployeeLocalServiceUtil it can detect the method that was created in the EmployeeLocalServiceImpl??
ReplyDeleteHi Nazib,
DeleteYou are correct. It will communicate with EmployeeLocalServiceImpl.
if I want to click the button three: insert,update,delete form properties and attributes of the button will look like?
ReplyDeleteThe above code is for insert into database if you want to edit or delete you have to add some code
DeleteCode works fine !!
ReplyDeleteAdvanced Java Books
html web design examples
ReplyDeletecss html - label text-transform uppercase
Investment funds services in UAE
ReplyDeleteFinancial funds services in UAE
Investment Companies in UAE
Looking For Investors in UAE
Asset Management Companies in UAE
Dubai funds
Funds in Dubai
hi,
ReplyDeleteare you sure all code above is working?
1. where you define class Employee.java?
2. How you connect Liferay Service Builder with Portlet JSP?
3. where you put the database properties?
4. where is sample of EmployeeLocalServiceBaseImpl and counterLocalService?
arghhhhh....please guide us...
Good luck & keep writing such awesome content.
ReplyDeleteVirgin Linseed Oil BP
flaxseed oil
Approved Auditor in DAFZA
ReplyDeleteApproved Auditor in RAKEZ
Approved Auditor in JAFZA
i heard about this blog & get actually whatever i was finding. Nice post love to read this blog
Approved Auditor in DMCC
Great and that i have a dandy offer: How Much Is House Renovation Loan In Pag Ibig brick house exterior makeover
ReplyDelete