Laliwala IT Services

Laliwala IT Services
Website Development

Thursday, March 31, 2011

Adding database interaction, Retrieving records

Prepared by Aasif and Ahmed under the guidance of Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================
5. Adding database interaction - Retrieving records

5.1 Update BookLocalServicImpl.java
------------------------------------

add the new method

public List<Book> getAllBooks()
throws PortalException, SystemException, RemoteException {
return bookPersistence.findAll();
}

append the new import,

import java.util.List;

5.2 run ant build-service again to re-generate the corresponding Util classes
-----------------------------------------------------------------------------

a. execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"

b. execute "ant deploy" from ext-impl to compile and deploy all the generated file and make sure the build is SUCCESSFUL.


5.3 update "init.jsp"
---------------------

Make additional imports. The intent is to get access to generated service layer classes inside any of the jsp's of this portlet.

(location "ext/ext-web/docroot/html/portlet/ext/library")

<%@ page import="java.util.List" %>
<%@ page import="com.ext.portlet.library.model.Book" %>
<%@ page import="com.ext.portlet.library.service.BookLocalServiceUtil" %>








5.4 update "success.jsp" (append the below code)
------------------------------------------------

(location "ext/ext-web/docroot/html/portlet/ext/library")

<%
String bookTitle = request.getParameter("title");

// new line inserted.
List<Book> books = BookLocalServiceUtil.getAllBooks();
%>

<table align="center" cellspacing="10" cellpadding="3">
<tr>
<td style="font-weight:bold">Book Title: </td>
<td><%= bookTitle %></td>
</tr>
</table>

<!-- new code added -->
<table border="1">
<%
for (Book book : books) {
%>
<tr>
<td>Book Id: <%= book.getBookId()%></td>
<td>Title: <%= book.getTitle() %></td>
</tr>
<%
}
%>
</table>

5.5 ant deploy to tomcat
------------------------

a. run "ant deploy-fast" from "ext-web"

b. re-start the tomcat server

c. see the updates to the library portlet (the records are retrieved properly)

Exercise:

display the dateAdded column in the table where all the books are listed.


5.6 the problem with page re-fresh
----------------------------------

everything is working fine. try to refresh the page and see what happens.

duplicate record is getting added to the database. In the next section, we'll see how to handle this
















See that after refreshing duplicate record is added.

Check the table contents which shows duplicate recors.



2 comments:

  1. hi,
    I to have same problem, when ever the page refreshed new record in inserted in database.
    Please help me?

    ReplyDelete
  2. hi manasa

    to resolve the refresh problem perform the following
    in your AddBookAction.java file

    import com.liferay.portlet.ActionResponseImpl;

    and Comment out the forward line
    //setForward(req, "portlet.ext.library.success");

    replace with these lines

    PortletURL redirectURL = ((ActionResponseImpl) res).createRenderURL();
    redirectURL.setParameter("struts_action", "/ext/library/success");
    res.sendRedirect(redirectURL + "&title=" + bookTitle);

    it will work fine

    ReplyDelete