background image

Generate Tags Dynamically

<< CDATA Wrappers | Using the jsp:root Element >>
<< CDATA Wrappers | Using the jsp:root Element >>

Generate Tags Dynamically

TABLE 6­2
EL Operators and JSP Document-Compliant Alternative Notation
EL Operator
JSP Document Notation
<
lt
>
gt
<=
le
>=
ge
!=
ne
You can also use EL expressions with jsp:element to generate tags dynamically rather than
hard code them. This example could be used to generate an HTML header tag with a lang
attribute:
<jsp:element name=
"${content.headerName}"
xmlns:jsp=
"http://java.sun.com/JSP/Page">
<jsp:attribute name=
"lang">${content.lang}</jsp:attribute>
<jsp:body>${content.body}</jsp:body>
</jsp:element>
The name attribute identifies the generated tag's name. The jsp:attribute tag generates the
lang
attribute. The body of the jsp:attribute tag identifies the value of the lang attribute. The
jsp:body
tag generates the body of the tag. The output of this example jsp:element could be
<h1 lang=
"fr">Heading in French</h1>
As shown in
Table 6­1
, scripting elements (described in
Chapter 9, "Scripting in JSP Pages"
) are
represented as XML elements when they appear in a JSP document. The only exception is a
scriptlet expression used to specify a request-time attribute value. Instead of using <%=expr %>,
a JSP document uses %= expr % to represent a request-time attribute value.
The three scripting elements are declarations, scriptlets, and expressions.
A jsp:declaration element declares a scripting language construct that is available to other
scripting elements. A jsp:declaration element has no attributes and its body is the
declaration itself. Its syntax is
<jsp:declaration> declaration goes here </jsp:declaration>
A jsp:scriptlet element contains a Java program fragment called a scriptlet. This element has
no attributes, and its body is the program fragment that constitutes the scriptlet. Its syntax is
<jsp:scriptlet> code fragment goes here </jsp:scriptlet>
Creating a JSP Document
Chapter 6 · JavaServer Pages Documents
195