In this section, we will see how we are going to integrate our application with the persistence api of
service layer just got created. We will make use of the “addLMSBook” to persist the book that gets
added by the user.
Let us open our LibraryPortlet.java file and insert the below code to the “updateBook” method,
Also make sure that you have made the necessary imports to this java file.
Save all your changes and observe that the portlet is getting deployed properly.
Go to your form and add a book.
Confirm the book information is getting inserted to the database by opening the MySQL Query Browser.
You will see a new table “LMS_LMSBook”. Keeping adding more books through the form and see the records are getting inserted into this table.
Congratulations!! You have successfully integrated a service layer api with our application.
In the next section we are going to see how to retrieve the records of our table and show in a new
page, “list.jsp”.
Retrieving the records – use another API
1. create a new jsp file with name “list.jsp” under “/html/library” and insert the include for init.jsp
2. insert this code in the list.jsp
<%@ include file="/html/library/init.jsp" %>
<h1>List of books in our Library</h1>
3. Open view.jsp and insert a link to list.jsp,
<%
PortletURL listBooksURL = renderResponse.createRenderURL();
listBooksURL.setParameter("jspPage", "/html/library/list.jsp");
%>
<a href="<%= listBooksURL.toString() %>">Show all books »</a>
4. Go to the browser and check the new link is correctly taking you to the “List of books”
5. Add the following code to list.jsp to get the list of books and display
6. Go to the portlet in browser and click on “Show all books”, you will see all the books
getting listed out as below.
Congratulations!! you have successfully retrieved all the books of our library and displayed them on an new page. We have made use of two new API's of LMSBookLocalServiceUtil – getLMSBooksCount() and getLMSBooks(0, count).
Problem when you refresh the page
Now, you will observe a new problem when you do a page refresh by pressing “F5” immediately after the book gets inserted. Whenever you press “F5” a new record gets inserted to the database which is not a desired behavior. In this section, we will see how to get rid of this problem and also sensitizing you to take care of such issues during your real time development.
1. setting a redirectURL on your JSP
Open “update.jsp” and insert a hidden variable “redirectURL” as the first element of the AUI form.
2. Changes to the portlet class
Open LibraryPortlet.java and add the following lines to the end of the “updateBook” method.
3. Save all your changes and check the refresh problem got suitably addressed by now.
service layer just got created. We will make use of the “addLMSBook” to persist the book that gets
added by the user.
Let us open our LibraryPortlet.java file and insert the below code to the “updateBook” method,
LMSBook book = new LMSBookImpl();
// set primary key
long bookId = 0L;
try {
bookId =
CounterLocalServiceUtil.increment(
this.getClass().getName());
} catch (SystemException e) {
e.printStackTrace();
}
book.setBookId(bookId);
// set UI fields
book.setBookTitle(bookTitle);
book.setAuthor(author);
// set audit field(s)
book.setDateAdded(new Date());
// insert the book using persistence api
try {
LMSBookLocalServiceUtil.addLMSBook(book);
} catch (SystemException e) {
e.printStackTrace();
}
Also make sure that you have made the necessary imports to this java file.
Save all your changes and observe that the portlet is getting deployed properly.
Go to your form and add a book.
Confirm the book information is getting inserted to the database by opening the MySQL Query Browser.
You will see a new table “LMS_LMSBook”. Keeping adding more books through the form and see the records are getting inserted into this table.
Congratulations!! You have successfully integrated a service layer api with our application.
In the next section we are going to see how to retrieve the records of our table and show in a new
page, “list.jsp”.
Retrieving the records – use another API
1. create a new jsp file with name “list.jsp” under “/html/library” and insert the include for init.jsp
2. insert this code in the list.jsp
<%@ include file="/html/library/init.jsp" %>
<h1>List of books in our Library</h1>
3. Open view.jsp and insert a link to list.jsp,
<%
PortletURL listBooksURL = renderResponse.createRenderURL();
listBooksURL.setParameter("jspPage", "/html/library/list.jsp");
%>
<a href="<%= listBooksURL.toString() %>">Show all books »</a>
4. Go to the browser and check the new link is correctly taking you to the “List of books”
5. Add the following code to list.jsp to get the list of books and display
<%
int count = LMSBookLocalServiceUtil.getLMSBooksCount();
List<LMSBook> books =
LMSBookLocalServiceUtil.getLMSBooks(0, count);
%>
<table border="1" width="80%">
<tr>
<th>Book Title</th>
<th>Author</th>
<th>Date Added</th>
</tr>
<%
for (LMSBook book : books) {
%>
<tr>
<td><%= book.getBookTitle() %></td>
<td><%= book.getAuthor() %></td>
<td><%= book.getDateAdded() %></td>
</tr>
<%
}
%>
</table>
<br/><a href="<portlet:renderURL/>">« Go Back</a>
6. Go to the portlet in browser and click on “Show all books”, you will see all the books
getting listed out as below.
Congratulations!! you have successfully retrieved all the books of our library and displayed them on an new page. We have made use of two new API's of LMSBookLocalServiceUtil – getLMSBooksCount() and getLMSBooks(0, count).
Problem when you refresh the page
Now, you will observe a new problem when you do a page refresh by pressing “F5” immediately after the book gets inserted. Whenever you press “F5” a new record gets inserted to the database which is not a desired behavior. In this section, we will see how to get rid of this problem and also sensitizing you to take care of such issues during your real time development.
1. setting a redirectURL on your JSP
Open “update.jsp” and insert a hidden variable “redirectURL” as the first element of the AUI form.
<aui:input type="hidden" name="redirectURL" value="<%= renderResponse.createRenderURL().toString() %>"/>
2. Changes to the portlet class
Open LibraryPortlet.java and add the following lines to the end of the “updateBook” method.
// gracefully redirecting to the default portlet view
String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
actionResponse.sendRedirect(redirectURL);
3. Save all your changes and check the refresh problem got suitably addressed by now.


