Most real-world tag files are controlled through attribute values set by the page author.
Example 11-2. Using attributes in a tag file (motd.tag)
<%@ tag body-content="empty" %>
<%@ attribute name="category" required="true" %>
<%@ taglib prefix="c" uri="" %>
${mmb.message}
Each attribute must be declared with an attribute directive in a tag file.The value the page author assigns to an attribute shows up as a page scope variable in the tag file.
Using Undeclared Attributes
The tag files can use the tag directive's "dynamic-attribute" attribute to accept undeclared attributes.This attribute declares that the tag file accepts any custom action element attribute.The attribute value is the name of a local page scope variable that holds a collection(a Map) with all undeclared attribute names and values.
Example 11-3. Using undeclared attributes in a tag file (headers.tag)
<%@ tag body-content="empty" dynamic-attributes="dynattrs" %>
<%@ attribute name="caption" required="true" %>
<%@ taglib prefix="c" uri="" %>
${a.key}="${a.value}"
>
${caption}
Name |
Value |
---|
${h.key} |
${h.value} |
The dynamic-attributes attribute declares a variable named dynattrs to hold the undecalred attributes(in jsp file is the params flowed by the tag name).
This is how you can use the tag file in a JSP page:
<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
Headers border="1" cellspacing="0" cellpadding="5" />
The action element for the tag file defines values for the mandatory 'caption' attribute plus three undeclared attributes:border,cellspacing,and cellpadding.
阅读(589) | 评论(0) | 转发(0) |