<jsp:attribute>当使用在<jsp:element>之中时,设置动态定义的XML元素属性。
语法
<jsp:attribute trim="true | false"> ...... </jsp:attribute >
它有两个属性:name 和trim。其中name 的值就是标签的属性名称。trim 可为true 或false。假若为true 时,<jsp:attribute>本体内容的前后空白,将被忽略;反之,若为false,前后空白将不被忽略。trim 的默认值为true。
例子
下面例子使用 Fragment 实现页面布局,
//首先在 WEB-INF/tags 文件夹中创建 template.tag 文件 <%@tag description="template 1" pageEncoding="UTF-8"%> <%@attribute name="header" fragment="true" %> <%@attribute name="footer" fragment="true" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <jsp:invoke fragment="header"/> <jsp:doBody/> <jsp:invoke fragment="footer"/> </body> </html>
在 tag 文件头部申明了两个 attribute 分别是 header 和 footer。在 <body> 标签中调用了这两个 attribute 所对应的 fragment。jsp:invoke 和 jsp:doBody 中的具体内容会被 jsp 中的内容替换。现在编写 index.jsp。
//创建index.jsp文件 <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="t" tagdir="/WEB-INF/tags/"%> http://write.blog.csdn.net/postedit <t:template> <jsp:attribute name="header"> 这里的内容显示在头部。 </jsp:attribute> <jsp:attribute name="footer"> 这里的内容显示在尾部。 </jsp:attribute> <jsp:body> 这里显示正文内容:Hello World! </jsp:body> </t:template>
jsp:attribute 标签中的内容将会替换 template.tag 中 jsp:invoke 的内容,name 属性对应 fragment 属性。如果访问 index.jsp 页面,可以看到显示的内容会按照 template.tag 中设计的样式来进行布局。
总结
1. 当使用在<jsp:element>之中时,它可以定义XML 元素的属性
2. <jsp:attribute>可以用来设定标准或自定义标签的属性值。