Java Reference
In-Depth Information
<th><c:out value="${property.name}"/></th>
</c:forEach>
</tr>
<c:forEach var="result" items="${requestScope.results}">
<tr>
<c:forEach var="property" items="${properties}">
<td><d:getProperty name="${property.name}"
item="${result}"/></td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
We use both JSTL tags and custom taglibs to write the JSP . The JSTL tag library is a
standard set of useful and generic tags. It's divided into several categories (core, XML ,
formatting, and SQL ). The category used here is the core, which provides output, man-
agement of variables, conditional logic, loops, text imports, and URL manipulation.
We also write two custom tags, <d:properties> and <d:getProperty> , which are
used to extract information from the DynaBeans. <d:properties> extracts the name
of all properties of a DynaBean, and <d:getProperty> extracts the value of a given
DynaBean property.
There are two reasons for writing these custom tags. The primary reason is that it
isn't possible to extract DynaBean information without ( ouch! ) embedding Java code
in the JSP (at least not with the current implementation of the JSTL tags and the Dyna-
Bean package). The second reason is that it gives us a chance to write and unit test cus-
tom taglibs of our own.
W RITING THE C ACTUS TEST
Now let's write a Cactus ServletTestCase for the JSP . The callView method from the
AdminServlet forwards control to the Results View JSP , as shown in listing 14.10.
Listing 14.10 shows a unit test for callView that sets up the DynaBean objects in the
Request , calls callView , and then verifies that the JSP output is what we expect.
Listing 14.10
TestAdminServlet.java: unit tests for results.jsp
[...]
public class TestAdminServlet extends ServletTestCase {
private Collection createCommandResult() throws Exception {
List results = new ArrayList();
B
DynaProperty[] props = new DynaProperty[] {
new DynaProperty("id", String.class),
new DynaProperty("responsetime", Long. class )
};
BasicDynaClass dynaClass = new
BasicDynaClass("requesttime", null ,props);
 
 
 
Search WWH ::




Custom Search