JSTL Books
print
(last updated:
Nov 14, 2009)
print
This application has exactly the same behavior as that
of Java Web Books. The JSP files are
different in that they exclusively use tags to do everything
no "scripting" is used in any JSP file.
As a consequence, other scripting-related features such as
"page import" tags are no longer relevant.
You can either download the
JSTLBooks.zip archive
for use in NetBeans, or the file
JSTLBooks.war for
use in Eclipse. With NetBeans you must add the following JAR
files into the Library; alternatively,
install the MySQL, Hibernate, CommonsLang and JSTL libraries.
Hibernate essentials:
antlr-2.7.6.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
hibernate-core-3.3.1.GA.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
slf4j-simple-1.5.8.jar
Commons/Lang:
commons-lang-2.4.jar
MySQL JDBC Driver:
mysql-connector-java-5.1.10-bin.jar
JSTL:
jstl.jar
standard.jar
Added Bean class
The JSTL & EL tags and function provide ways to replace every scripting
detail used in
Java Web Books
except this code used to generate a book listing:
<%
Session sess = db.getSession();
Criteria crit = sess.createCriteria(Book.class);
List<Book> books = crit.list();
%>
Although there are a number of possible solutions, we decide to add a new
bean class:
package models;
import java.util.*;
import org.hibernate.*;
import org.hibernate.criterion.Order;
public class Books {
private DB db = new DB();
private Order order = Order.asc("id");
public void setOrderField(String field) {
this.order = Order.asc(field);
}
public Books() {
}
public List<Book> getList() {
Session sess = db.getSession();
Criteria crit = sess.createCriteria(Book.class);
crit.addOrder(order);
List<Book> books = crit.list();
return books;
}
}
The Books class will provide a means of "hiding"
the db bean which we have
used throughout. The Books class, unlike the Hibernate ORM,
is not meant to be a general purpose class for accessing the books table;
it is simply a means for providing the functionality we need in order
to avoid JSP scripting; it must be adapted as needed by the application.
The replacement for the above code becomes:
<jsp:useBean id="books" scope="session" class="models.Books" />
...
${books.list} <!-- is the same as "books" in the previous version -->
Additionally, the Bean class is written
is a way which allows
the getList function to generate an "ordered" listing
by virtue of the additional setOrderField
function (ascending only).
The JSP files
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table class="nav">
<tr>
<c:forEach var="entry" items="${nav.links}" >
<td> <a href="${entry.key}">${entry.value}</a> </td>
</c:forEach>
</tr>
</table>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/functions" %>
<jsp:useBean id="books" scope="session" class="models.Books" />
<jsp:useBean id="nav" scope="session" class="util.Nav" />
<jsp:useBean id="cart" scope="session" class="java.util.HashMap" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<c:set var="page_title" value="JSTL Books" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${page_title}</title>
<link rel="stylesheet" href="css/common.css" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/errors.js"></script>
<script type="text/javascript" src="js/display.js"></script>
<script type="text/javascript" src="js/cart.js"></script>
</head>
<body>
<c:set target="${nav}" property="choice" value="ANON" />
<%@ include file="nav.jsp" %>
<h1>${page_title}</h1>
<select id="sel">
<option value="0">-- choose --</option>
<c:set target="${books}" property="orderField" value="title" />
<c:forEach var="book" items="${books.list}">
<option value="${book.id}">${f:escapeXml(book.title)}</option>
</c:forEach>
</select>
<br /><br />
<div id="output">
<table cellpadding="5px">
<tr><td>id:</td> <td id="id"></td></tr>
<tr><td>title:</td> <td id="title"></td></tr>
<tr><td>type:</td> <td id="type"></td></tr>
<tr><td>qty:</td> <td id="qty"></td></tr>
</table>
<button id="addtocart">Add To Cart</button>
</div>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="cart" scope="session" class="java.util.HashMap" />
<jsp:useBean id="nav" scope="session" class="util.Nav" />
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="page_title" value="Cart" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${page_title}</title>
<link rel="stylesheet" href="css/common.css" type="text/css" />
</head>
<body>
<c:set target="${nav}" property="choice" value="ANON" />
<%@ include file="nav.jsp" %>
<h1>${page_title}</h1>
${cart}
</body>
</html>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="book" uri="/WEB-INF/booktags"%>
<jsp:useBean id="authadmin" scope="session" class="util.AuthInfo" />
<c:if test="${!authadmin.status}">
<jsp:forward page="login.jsp" />
</c:if>
<!-- otherwise, basically the same as index.jsp, with a delete button
added and the cart button removed
-->
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="authadmin" scope="session" class="util.AuthInfo" />
<jsp:useBean id="nav" scope="session" class="util.Nav" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<c:set var="page_title" value="Login" />
<html>
<head>
<title>${page_title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/common.css" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/errors.js"></script>
<script type="text/javascript" src="js/login.js"></script>
</head>
<body>
<c:set target="${nav}" property="choice" value="LOGIN" />
<%@ include file="nav.jsp" %>
<h1>${page_title}</h1>
<form id="auth" action="Validate" autocomplete="off">
Password: <input name="pwd" type="password" />
<input type="submit" value="Enter" />
</form>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="authadmin" scope="session" class="util.AuthInfo" />
<c:set target="${authadmin}" property="status" value="false" />
<c:redirect url="." />
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="page_title" value="Add A Book" />
<!-- There is little difference between this version and the
version in WebBooks
-->
© Robert M. Kline