Explanation:-In this article we will explain how to perform CRUD Operations in Liferay Custom portlet. We will also explain the how to use service builder tool to perform all the operations one by one. At the end of this blog we will understand how to perform Create, Read, Update , Delete and Search Operations on Liferay Entity.
Step1:- create a portlet named as"registrationform".
Step2:-create a service builder as shown in figure.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.reg">
<author>E00012</author>
<namespace>C</namespace>
<entity name="Student" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="studentId" type="long" primary="true" />
<!-- Audit fields -->
<column name="firstname" type="String" />
<column name="lastName" type="String" />
<column name="branch" type="String" />
<column name="age" type="int" />
<column name="email" type="String"></column>
</entity>
</service-builder>
Step3:- Now build the service and after that create a package,class and jsp as shown in the figure.
Step4:- In view.jsp write the code
Note- you can download the code from the given link at the bottom.
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<h1>Registration form submission</h1>
<%
PortletURL addStudent = renderResponse.createRenderURL();
addStudent.setParameter("mvcPath", "/html/regform/add_student.jsp");
PortletURL updateStudent = renderResponse.createRenderURL();
updateStudent.setParameter("mvcPath", "/html/regform/update_by_id.jsp");
PortletURL dislayStudent = renderResponse.createRenderURL();
dislayStudent.setParameter("mvcPath", "/html/regform/display_student.jsp");
PortletURL updateStudentURL=renderResponse.createRenderURL();
updateStudentURL.setParameter("mvcPath", "/html/regform/update_student.jsp");
PortletURL searchByName=renderResponse.createRenderURL();
searchByName.setParameter("mvcPath", "/html/regform/search_by_name.jsp");
%>
<br/>
<a href="<%=addStudent.toString()%>">Add Student</a><br/>
<a href="<%=updateStudent.toString()%>">Update Student</a><br/>
<a href="<%=dislayStudent.toString()%>">Display Student</a><br/>
<a href="<%=searchByName.toString()%>">Search</a>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<h1>Registration form submission</h1>
<%
PortletURL addStudent = renderResponse.createRenderURL();
addStudent.setParameter("mvcPath", "/html/regform/add_student.jsp");
PortletURL updateStudent = renderResponse.createRenderURL();
updateStudent.setParameter("mvcPath", "/html/regform/update_by_id.jsp");
PortletURL dislayStudent = renderResponse.createRenderURL();
dislayStudent.setParameter("mvcPath", "/html/regform/display_student.jsp");
PortletURL updateStudentURL=renderResponse.createRenderURL();
updateStudentURL.setParameter("mvcPath", "/html/regform/update_student.jsp");
PortletURL searchByName=renderResponse.createRenderURL();
searchByName.setParameter("mvcPath", "/html/regform/search_by_name.jsp");
%>
<br/>
<a href="<%=addStudent.toString()%>">Add Student</a><br/>
<a href="<%=updateStudent.toString()%>">Update Student</a><br/>
<a href="<%=dislayStudent.toString()%>">Display Student</a><br/>
<a href="<%=searchByName.toString()%>">Search</a>
Step5:- create a add_student.jsp
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletMode"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<%-- <portlet:renderURL var="homeURL"></portlet:renderURL>
<portlet:actionURL var="addStudentActionURL" windowState="normal"
name="addStudent">
</portlet:actionURL> --%>
<%
PortletURL homeURL = renderResponse.createRenderURL();
PortletURL addStudentActionURL = renderResponse.createActionURL();
addStudentActionURL.setParameter(ActionRequest.ACTION_NAME, "addStudent");
%>
<h2>Add Student Form here !</h2>
<a href="<%=homeURL.toString()%>">Home</a>
<br />
<br />
<aui:form action="<%=addStudentActionURL.toString()%>" name="studentForm" method="POST">
<aui:input name="firstName" >
<aui:validator name="required"/>
<aui:validator name="alpha"/>
</aui:input>
<aui:input name="lastName" >
<aui:validator name="required"/>
<aui:validator name="alpha"/>
</aui:input>
<aui:input name="branch" >
<aui:validator name="required"/>
<aui:validator name="alpha"/>
</aui:input>
<aui:input name="age">
<aui:validator name="required"/>
<aui:validator name="alpha"/>
</aui:input>
<aui:input name="email" >
<aui:validator name="required"/>
<aui:validator name="alpha"/>
</aui:input>
<aui:button type="submit" name="" value="Submit"></aui:button>
</aui:form>
Step6:- put the jsp code as shown in the figure . the following is:-
view.jsp
add_student.jsp
display_search_student.jsp
display_student.jsp
search_by_name.jsp
student_details.jsp
update_by_id.jsp
update_student.jsp
Step7:- write the java code as below
package com.reg;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.reg.model.Student;
import com.reg.service.StudentLocalServiceUtil;
/**
* Portlet implementation class Regform
*/
public class Regform extends MVCPortlet {
public void addStudent(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException,
PortletException, SystemException {
System.out.println("hello to this page");
long studentId;
studentId = CounterLocalServiceUtil.increment();
String firstName = ParamUtil.getString(actionRequest, "firstName");
System.out.println(firstName);
String lastName = ParamUtil.getString(actionRequest, "lastName");
String branch = ParamUtil.getString(actionRequest, "branch");
int age = (int) ParamUtil.getLong(actionRequest, "age");
String email=ParamUtil.getString(actionRequest, "email");
System.out.println(email+" yes");
Student student = null;
student = StudentLocalServiceUtil.createStudent(studentId);
student.setStudentId(studentId);
student.setFirstname(firstName);
student.setLastName(lastName);
student.setBranch(branch);
student.setAge(age);
student.setEmail(email);
StudentLocalServiceUtil.addStudent(student);
actionResponse.setRenderParameter("mvcPath",
"/html/regform/display_student.jsp");
}
public void updateStudent(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, SystemException, PortalException {
System.out.println("yes it is progress");
long studentId=ParamUtil.getLong(actionRequest, "studentId");
String firstName = ParamUtil.getString(actionRequest, "firstName");
String lastName = ParamUtil.getString(actionRequest, "lastName");
String branch = ParamUtil.getString(actionRequest, "branch");
int age = ParamUtil.getInteger(actionRequest, "age");
String email=ParamUtil.getString(actionRequest, "email");
System.out.println("update");
Student student = StudentLocalServiceUtil.getStudent(studentId);
if (student != null) {
// fill update information
student.setFirstname(firstName);
student.setLastName(lastName);
student.setBranch(branch);
student.setAge(age);
student.setEmail(email);
System.out.println("yes");
}
student = StudentLocalServiceUtil.updateStudent(student);
actionResponse.setRenderParameter("mvcPath",
"/html/regform/display_student.jsp");
}
public void deleteStudent(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortalException, SystemException {
long studentId = ParamUtil.getLong(actionRequest, "studentId");
System.out.println(studentId);
Student student = StudentLocalServiceUtil.deleteStudent(studentId);
actionResponse.setRenderParameter("mvcPath",
"/html/regform/display_student.jsp");
}
public void getStudent(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException,PortalException, SystemException {
long studentId = ParamUtil.getLong(actionRequest, "studentId");
System.out.println(studentId);
String cmd = ParamUtil.getString(actionRequest, "cmd");
Student student = StudentLocalServiceUtil.getStudent(studentId);
actionResponse.setRenderParameter("mvcPath",
"/html/regform/update_student.jsp");
}
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete