Accessing Attribute Values
时间:2008-09-03 来源:zyme007
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="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="mmb" class="com.ora.jsp.beans.motd.MixedMessageBean" />
<c:set target="${mmb}" property="category" value="${category}" />
${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="http://java.sun.com/jsp/jstl/core" %>
<table
<c:forEach items="${dynattrs}" var="a">
${a.key}="${a.value}"
</c:forEach>
>
<caption>${caption}</caption>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<c:forEach items="${header}" var="h">
<tr>
<td>${h.key}</td>
<td>${h.value}</td>
</tr>
</c:forEach>
</table>
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" %>
<html>
<head>
<title>Headers</title>
</head>
<body bgcolor="white">
<my:headers caption="Request Headers"
border="1" cellspacing="0" cellpadding="5" />
</body>
</html>
The action element for the tag file defines values for the mandatory 'caption' attribute plus three undeclared attributes:border,cellspacing,and cellpadding.
Example 11-2. Using attributes in a tag file (motd.tag)
<%@ tag body-content="empty" %>
<%@ attribute name="category" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="mmb" class="com.ora.jsp.beans.motd.MixedMessageBean" />
<c:set target="${mmb}" property="category" value="${category}" />
${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="http://java.sun.com/jsp/jstl/core" %>
<table
<c:forEach items="${dynattrs}" var="a">
${a.key}="${a.value}"
</c:forEach>
>
<caption>${caption}</caption>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<c:forEach items="${header}" var="h">
<tr>
<td>${h.key}</td>
<td>${h.value}</td>
</tr>
</c:forEach>
</table>
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" %>
<html>
<head>
<title>Headers</title>
</head>
<body bgcolor="white">
<my:headers caption="Request Headers"
border="1" cellspacing="0" cellpadding="5" />
</body>
</html>
The action element for the tag file defines values for the mandatory 'caption' attribute plus three undeclared attributes:border,cellspacing,and cellpadding.
相关阅读 更多 +