这个工程的步骤和『建立HelloWorldSimpleTag工程』类似,但不同的是它的标签中含有属性(atrribute)。
1. 标签类如下,它是扩展自 SimpleTagSupport 类的,且符合 JavaBean 的书写方式。
public class RepeatSimpleTag extends SimpleTagSupport {
private int num;
public void doTag() throws JspException, IOException {
for (int i = 0; i < num; i++) {
getJspContext().setAttribute("count", String.valueOf(i + 1));//设置输出
getJspBody().invoke(null);
}
}
public void setNum(int num) {
this.num = num;
}
}
2. 以下是标签描述符中的内容,有关标签描述符,必须参考这里的官方资料:
<tag>
<description>repeat demo</description>
<name>repeatNum</name>
<tag-class>hello.RepeatSimpleTag</tag-class>
<!--
scriptless: The body accepts only static text, EL expressions, and
custom tags. No scripting elements are allowed.
-->
<body-content> scriptless </body-content>
<variable><!-- 2012.12.27补充:variable 元素可以删除,并不影响运行。-->
<description>Current invocation count (1 to num)</description>
<!-- EL 调用的名字,变量的名称,即所谓输出的名称 -->
<name-given>count</name-given>
</variable>
<attribute>
<!-- 属性的名称即所谓输入的名称 -->
<name>num</name>
<required>true</required>
<!-- 用于实时调用,即 EL 调用 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
注:2012.12.27 补充,目前的运行情况看来,variable 元素可以删除。软件环境是 apache-tomcat-7.0.34 & jdk1.7.0_10
3. 在具体的 JSP 页面中以如下方式引用:
<iridium:repeatNum num="5">
${count}
</iridium:repeatNum>
注:在某些文档的描述中,标签中的属性(attribute)被称为输入参数,如这里的 num,而标签体中$符号引用的变量(variable)的值,被成为输出,如这里的 ${count}。)
4. 最後得到的输出是:
1 2 3 4 5