Sunday 7 July 2013

How to update and delete data in database using search container in liferay?

Hi Liferay members , in my previous post i have explained to retrieve data from database this time i am going to show you how to update those data from database .
Just follow this simple steps

list.jsp 

1)Open list.jsp where you have written code for search container to retrieve data and this following line in that container.

<liferay-ui:search-container-column-jsp path="/button.jsp" />

button.jsp

1)Now create button.jsp and add following snippet of code


<%ResultRow rslt=(ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW) ;

Foo f=(Foo)rslt.getObject();

String prk=String.valueOf(f.getFooId());

%>
//Button menu tag
<liferay-ui:icon-menu>

//Create “editProduct” method in Action file
<portlet:actionURL var="editURL" name="editProducts">
<portlet:param name="edit" value="<%=prk %>"/>
</portlet:actionURL>

// ”EDIT” button for Update event. 
<liferay-ui:icon image="edit" message="Edit" url="<%=editURL.toString() %>"/>
</liferay-ui:icon-menu>

//Creates "deleteproducts" method in action file
<portlet:actionURL var="deleteURL" name="deleteproducts">
<portlet:param name="edit" value="<%=prk %>"/>
</portlet:actionURL>
<liferay-ui:icon-delete url="<%=deleteURL.toString() %>" />




Controller Class

1)open your Java class create method  in Action file as shown below


public void editProducts(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalException, SystemException 

 {

 String str=arrq.getParameter("edit");

 long l=Long.valueOf(str);

 //retrieve table using primary key
 Foo fa = FooLocalServiceUtil.getFoo(l);
 System.out.println("sdv"+ fa);

//Set Attribute which has all values of specified pk.
 arrq.setAttribute("edit",fa);
 arrs.setRenderParameter("jspPage", "/edit.jsp");
 
 }

edit.jsp

1)Now create a new jsp which has update form.Here is edit.jsp page code

//Get attribute that is fetch in above action class
<jsp:useBean id="edit" type="com.sample.model.Foo" scope="request" />

//Create update method updatenameURL to update
<portlet:actionURL name="updatenameURL" var="updatenameURL" />

<aui:form action="<%=updatenameURL.toString() %>" method="post">
<aui:input name="edit" value="<%=edit.getFooId() %>" type="hidden" />
<aui:input name="foo" value="<%=edit.getUserName() %>"/>
<aui:input name="title" value="<%=edit.getTitle() %>" />
<aui:button type="submit" value="Save"/>
</aui:form>


Controller class

1)Now open Controller class and create a method to update.Here is snippet of code


public void updatenameURL(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalException, SystemException {

 String str = arrq.getParameter("foo");

 String str1 = arrq.getParameter("title");

 String str2 = arrq.getParameter("edit");
 long l=Long.valueOf(str2);
//Fetch data using primary key 
 Foo itm=FooLocalServiceUtil.getFoo(l);

//Set Updated data

                   itm.setUserName(str);
 itm.setTitle(str1);

//update method with new data
 FooLocalServiceUtil.updateFoo(itm);

//Redirect to search containe page
 arrs.setRenderParameter("JspPage", "/list.jsp");
 
 
 }

2)Now create deleteproducts method in this Action class and here is its code

public void deleteproducts(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalE6tyxception, SystemException
 {
 String str2 = arrq.getParameter("edit");
 long l =Long.valueOf(str2);
 FooLocalServiceUtil.deleteFoo(l);
 arrs.setRenderParameter("JspPage", "/list.jsp");
 
 }

2)If everything goes fine,and if you concentrate on this program then you will get what you needed.



If any query plz do comment or mail us at:
chiragmsc007it@gmail.com
mehdisunasara@gmail.com

1 comment: