JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»


Iteration, especially enumerating some value, can be very declarative, and, as we’ve seen, declarative tasks are easily performed with tags. For example, by using iteration tags we can modify the previous JSP fragment:


<table>


<tr>


<th> Header </th> <th> Value </th>


</tr>


<iter:foreach id=»name»


type=»String»


object=n<%= request %>»


property=»headerNames» />


<tr>


<td> <%= name %> </td>


<td>


<bean:show object=»<%= request %>» property=»header» index=»<%= name %>»/>


</td>


</tr>


<iter:foreach>


</table>


This is obviously quite an improvement.


Why should we bother creating special iteration tags when a two-line scriptlet hardly seems demanding for a Java developer? Again, we can’t forget that the goal of building custom tag libraries is to make it possible for non-Java developers (presenta-tion/HTML developers) to build complex sites. Though iteration using scriptlets may not be complex for the Java programmer, it does require the JSP developer to:


■    Know how to iterate on different Java types—Enumerations, Iterators, arrays, and so forth. To further complicate the situation, iteration methods usually return an Object that the JSP developer will have to cast.


■    Position the curly brackets in the correct location. If the JSP developer forgets a curly bracket, the JSP compilation will fail, usually with a relatively obscure error message.


■    Maintain and debug yet another portion of Java code.


As a result, iteration tags are necessary to enhance the effectiveness of our JavaBean tags and to keep our JSPs scriptlet-free.


This chapter explores iteration with tags and shows how to build JSP custom tags that perform iteration for us. We’ll start with a brief introduction to iterating with custom JSP tags and discuss their design principles; later, we will develop iteration tags to handle cases in which we wish to iterate over Java’s common object containers.

Скачать в pdf «JSP Tag Libraries»