background image

CDATA Wrappers

<< Creating Static and Dynamic Content | Generate Tags Dynamically >>
<< Creating Static and Dynamic Content | Generate Tags Dynamically >>

CDATA Wrappers

123
The jsp:text tag must not contain any other elements. Therefore, if you need to nest a tag
inside jsp:text, you must wrap the tag inside CDATA.
You also need to use CDATA if you need to output some elements that are not well-formed. The
following example requires CDATA wrappers around the blockquote start and end tags because
the blockquote element is not well formed. This is because the blockquote element overlaps
with other elements in the example.
<c:forEach var=
"i" begin="1" end="${x}">
<![CDATA[<blockquote>]]>
</c:forEach>
...
<c:forEach var=
"i" begin="1" end="${x}">
<![CDATA[</blockquote>]]>
</c:forEach>
Just like JSP pages, JSP documents can generate dynamic content using expressions language
(EL) expressions, scripting elements, standard actions, and custom tags. The books.jspx
document uses EL expressions and custom tags to generate the XML book data.
As shown in this snippet from books.jspx, the c:forEach JSTL tag iterates through the list of
books and generates the XML data stream. The EL expressions access the JavaBeans
component, which in turn retrieves the data from the database:
<c:forEach var=
"book" begin="0" items="${bookDB.books}">
<book id=
"${book.bookId}" >
<surname>${book.surname}</surname>
<firstname>${book.firstName}</firstname>
<title>${book.title}</title>
<price>${book.price}</price>
<year>${book.year}</year>
<description>${book.description}</description>
<inventory>${book.inventory}</inventory>
</book>
</c:forEach>
When using the expression language in your JSP documents, you must substitute alternative
notation for some of the operators so that they will not be interpreted as XML markup.
Table 6­2
enumerates the more common operators and their alternative syntax in JSP
documents.
Creating a JSP Document
The Java EE 5 Tutorial · September 2007
194