Types of Tags
Simple tags are invoked using XML syntax. They have a start tag and an end tag, and possibly a body:
<tt:tag>body
</tt:tag>A custom tag with no body is expressed as follows:
<tt:tag /> or <tt:tag></tt:tag>Tags with Attributes
A simple tag can have attributes. Attributes customize the behavior of a custom tag just as parameters customize the behavior of a method. There are three types of attributes:
Simple Attributes
Simple attributes are evaluated by the container before being passed to the tag handler. Simple attributes are listed in the start tag and have the syntax
attr="value"
. You can set a simple attribute value from aString
constant, or an expression language (EL) expression, or by using ajsp:attribute
element (see jsp:attribute Element). The conversion process between the constants and expressions and attribute types follows the rules described for JavaBeans component properties in Setting JavaBeans Component Properties.简单属性在被传递给标签句柄之前,jsp容器就对其进行处理了。简单属性被列在开始标签内,语法是 attr=value.可以通过字符串常量,或EL表达式,或者使用jsp:attribute 元素对属性赋值。常量和表达式转换为属性类型时要遵从javabean的属性规则。
The Duke's Bookstore page
bookcatalog.jsp
calls thecatalog
tag, which has two attributes. The first attribute, a reference to a book database object, is set by an EL expression. The second attribute, which sets the color of the rows in a table that represents the bookstore catalog, is set with aString
constant.Fragment Attributes
A JSP fragment is a portion of JSP code passed to a tag handler that can be invoked as many times as needed. You can think of a fragment as a template that is used by a tag handler to produce customized content. Thus, unlike a simple attribute which is evaluated by the container, a fragment attribute is evaluated by a tag handler during tag invocation.
To declare a fragment attribute, you use the
fragment
attribute of theattribute
directive (see Declaring Tag Attributes in Tag Files) or use thefragment
subelement of theattribute
TLD element (see Declaring Tag Attributes for Tag Handlers). You define the value of a fragment attribute by using ajsp:attribute
element. When used to specify a fragment attribute, the body of thejsp:attribute
element can contain only static text and standard and custom tags; it cannot contain scripting elements (see Chapter 16).JSP fragments can be parametrized via expression language (EL) variables in the JSP code that composes the fragment. The EL variables are set by the tag handler, thus allowing the handler to customize the fragment each time it is invoked (see Declaring Tag Variables in Tag Files, and Declaring Tag Variables for Tag Handlers).
The
catalog
tag discussed earlier accepts two fragments:normalPrice
, which is displayed for a product that's full price, andonSale
, which is displayed for a product that's on sale.<sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike><fmt:formatNumber value="${price}" type="currency"/></strike><br/> <font color="red"><fmt:formatNumber value="${salePrice}" type="currency"/></font> </jsp:attribute> </sc:catalog>The tag executes the
normalPrice
fragment, using the values for theprice
EL variable, if the product is full price. If the product is on sale, the tag executes theonSale
fragment using theprice
andsalePrice
variables.Dynamic Attributes
A dynamic attribute is an attribute that is not specified in the definition of the tag. Dynamic attributes are used primarily by tags whose attributes are treated in a uniform manner but whose names are not necessarily known at development time.
For example, this tag accepts an arbitrary number of attributes whose values are colors and outputs a bulleted list of the attributes colored according to the values:
You can also set the value of dynamic attributes using an EL expression or using the
jsp:attribute
element.jsp:attribute Element
The
jsp:attribute
element allows you to define the value of a tag attribute in the body of an XML element instead of in the value of an XML attribute.For example, the Duke's Bookstore template page
screendefinitions.jsp
usesjsp:attribute
to use the output offmt:message
to set the value of thevalue
attribute oftt:parameter
:... <tt:screen id="/bookcatalog"> <tt:parameter name="title" direct="true"> <jsp:attribute name="value" > <fmt:message key="TitleBookCatalog"/> </jsp:attribute> </tt:parameter> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookcatalog.jsp" direct="false"/> </tt:screen> ...
jsp:attribute
accepts aname
attribute and atrim
attribute. Thename
attribute identifies which tag attribute is being specified. The optionaltrim
attribute determines whether or not whitespace appearing at the beginning and end of the element body should be discarded. By default, the leading and trailing whitespace is discarded. The whitespace is trimmed when the JSP page is translated. If a body contains a custom tag that produces leading or trailing whitespace, that whitespace is preserved regardless of the value of thetrim
attribute.An empty body is equivalent to specifying "" as the value of the attribute.
The body of
jsp:attribute
is restricted according to the type of attribute being specified:
- For simple attributes that accept an EL expression, the body can be any JSP content.
- For simple attributes that do not accept an EL expression, the body can contain only static text.
- For fragment attributes, the body must not contain any scripting elements (see Chapter 16).
Tags with Bodies
A simple tag can contain custom and core tags, HTML text, and tag-dependent body content between the start tag and the end tag.
In the following example, the Duke's Bookstore application page
bookshowcart.jsp
uses the JSTLc:if
tag to print the body if the request contains a parameter namedClear
:<c:if test="${param.Clear}"> <font color="#ff0000" size="+2"><strong> You just cleared your shopping cart! </strong><br> <br></font> </c:if>jsp:body Element
You can also explicitly specify the body of a simple tag by using the
jsp:body
element. If one or more attributes are specified with thejsp:attribute
element, thenjsp:body
is the only way to specify the body of the tag. If one or morejsp:attribute
elements appear in the body of a tag invocation but you don't include ajsp:body
element, the tag has an empty body.Tags That Define Variables
A simple tag can define an EL variable that can be used within the calling page. In the following example, the
iterator
tag sets the value of the EL variabledepartmentName
as it iterates through a collection of department names.<tlt:iterator var="departmentName" type="java.lang.String" group="${myorg.departmentNames}"> <tr> <td><a href="list.jsp?deptName=${departmentName}"> ${departmentName}</a></td> </tr> </tlt:iterator>Communication between Tags
Custom tags communicate with each other through shared objects. There are two types of shared objects: public and private.
In the following example, the
c:set
tag creates a public EL variable calledaVariable
, which is then reused byanotherTag
.Nested tags can share private objects. In the next example, an object created by
outerTag
is available toinnerTag
. The inner tag retrieves its parent tag and then retrieves an object from the parent. Because the object is not named, the potential for naming conflicts is reduced.The Duke's Bookstore page
template.jsp
uses a set of cooperating tags that share public and private objects to define the screens of the application. These tags are described in A Template Tag Library.
JSP tag学习
最新推荐文章于 2022-11-04 11:54:15 发布