Java Reference
In-Depth Information
Figure 14.3
Strategy to unit test JSPs with Cactus
information either from the container-implicit object (
HttpServletRequest
,
HttpServletResponse
, or
ServletConfig
) or from a taglib.
Still in
test
XXX
, we perform a
forward
to call the
JSP
under test. The
JSP
then
executes, getting the mock data set up in step 1.
2
Cactus calls
end
XXX
, passing to it the output from the
JSP
. This allows us to assert
the content of the output and verify that the data we set up found its way to the
JSP
output, in the correct location on the page.
3
14.4.4
Executing a JSP with SQL results data
Let's see some action on the Administration application. In the servlet section
(“Testing servlets and filters”), we defined that the results of executing the
SQL
query would be passed to the
JSP
by storing them as a collection of
DynaBean
objects
in the
HttpServletRequest
object. Thanks to the dynamic nature of DynaBeans, we
can easily write a generic
JSP
that will display any data contained in the DynaBeans.
DynaBeans provide metadata about the data they contain. We can create a generic
table with columns corresponding to the fields of the DynaBeans, as shown in list-
ing 14.9.
Listing 14.9
Results View JSP (results.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="
http://jakarta.apache.org/taglibs/core"
%>
<%@ taglib prefix="d" uri="/dynabeans" %>
<html>
<head>
<title>Results Page</title>
</head>
<body bgcolor="white">
<table border="1">
<d:properties var="properties"
item="${requestScope.results[0]}"/>
<tr>
<c:forEach var="property" items="${properties}">