Time for action - ­ implementing the list books by category page

The list books by category page (byCategory.jsp) will have a similar beginning as the list categories page. The page starts with the imports and the included initialization JSP.

<%@ page import="java.util.*"%>
	<%@ include file="init.inc.jsp" %>

This page takes a category as parameter:

<% // get category to browse, if none go to categories view
String category = request.getParameter("category");
if (category==null || category.equals("")) {
response.sendRedirect("index.jsp");
}
%>
<html>
<head>
<title>Bookshelf - Browse Category: <%= category %></title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>

Include the menu bar:

<%@ include file="menu.inc.jsp" %>
<h2>Bookshelf - Browse Category: <%= group %></h2>

Then perform the search operation, based on the passed group parameter:

<%@ include file="menu.inc.jsp" %>
<h2>Bookshelf - Browse Category: <%= category %></h2>
<%
Set<String> isbns =
sessionBean.getBookshelf().searchBooksByCategory(
sessionBean.getSessionId(), category); %>
<table class="BookList">
<%@ include file="bookListHeader.inc.jsp" %>
<% for (String isbn : isbns) { %>
<jsp:include page="bookListEntry.inc.jsp">
<jsp:param name="sessionBean" value="<%=sessionBean%>"/>
<jsp:param name="isbn" value="<%=isbn %>" />
</jsp:include>
<% } %>
</table>
</body>
</html>

For each result in the table, we've included a table row JSP chunk, which retrieves the book information from the bookshelf service and displays it as a separate row.

The result looks like this:

Time for action - ­ implementing the list books by category page

Of course, the style is mediocre. A lot more can be done with just a few cascading stylesheet classes, tag libraries, and so on, but learning JSP is outside the scope of this book.

A note on JSP imports

The JSP code that we've written so far uses the SessionBean to access the bookshelf service reference. When the bundle plugin analyzes the Java code, it will detect the need for the import of the package com.packtpub.felix.bookshelf.service.api, for example, and add it to the bundle Manifest.

However, the bundle plugin does not analyze the JSP's<%@ page import directives, for example, the JSP included when listing book entries, bookListEntry.inc.jsp, shown here. It imports the package com.packtpub.felix.bookshelf.inventory.api, which is not imported by the class SessionBean.

<%@page import="com.packtpub.felix.bookshelf.inventory.api.*" %>
<%@include file="init.inc.jsp" %>
<% String isbn = request.getParameter("isbn");
Book book = null;
try {
book = sessionBean.getBookshelf().getBook(
sessionBean.getSessionId(), isbn); %>
<tr class="BookListEntry">
<td><%= book.getCategory() %></td>
<td><%= book.getIsbn() %></td>
<td><%= book.getTitle() %></td>
<td><%= book.getAuthor() %></td>
<td><%= book.getRating() %></td>
</tr>
<% }
catch (BookNotFoundException e) { %>
<tr class="BookListEntry">
<td>-</td>
<td><%= isbn %></td>
<td>"<%= e.getMessage() %>"</td>
<td>-</td>
<td>-</td>
</tr>
<% }
%>

Imports of packages in JSPs must be manually declared in the POM (using Import-Package) when they are not imported by a Java class in the same bundle. If they are not, the generated servlet will fail at runtime because it cannot see the classes of that package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.141.115