开发带属性的标签,也比较简单,我们只需要为java标签类,声明属性,提供set方法,然后在tld文件中定义成带属性的标签就可以了。下面的例子,通过属性标签控制循环输出的次数。
package com.shizhan;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class SimpleTagDemo2 extends SimpleTagSupport {
int count;
@Override
public void doTag() throws JspException, IOException {
JspFragment j=this.getJspBody();
for(int i=0;i<count;i++)
{
j.invoke(null);
}
}
public void setCount(int count) {
this.count = count;
}
}
tld中的声明
<tag>
<description>simpledemo2</description>
<name>Att</name>
<tag-class>com.shizhan.SimpleTagDemo2</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name> count </name>
<required> true </required>
<rtexprvalue> true </rtexprvalue>
</attribute>
</tag>
jsp中
<shizhan:Att count="5">
attribute biaoqian
</shizhan:Att>